Java: Difference between revisions
Jump to navigation
Jump to search
Line 141: | Line 141: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Annotations === | ===Annotations=== | ||
Generally three layers, controller, service and repository with a model | Generally three layers, controller, service and repository with a model | ||
==== Model ===== | ====Model===== | ||
Models have @Entity | Models have @Entity | ||
Revision as of 05:30, 18 May 2020
JPA
This is a rough overview of configuration JPA with hibernate.
Pom.xml (Maven)
MySQL
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
Hibernate
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>6.0.0.Alpha5</version>
<type>pom</type>
</dependency>
Jta
<!-- https://mvnrepository.com/artifact/javax.transaction/jta -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
Spring Orm/JDBC
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
Persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="punit">
</persistence-unit>
</persistence>
Web.xml
Started by the container to listen for context events
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:/jpaContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
JpaContext.xml
Initial context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx "
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context-annotation-config />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
Adding EntityManager and Properties
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="perstistenceUnitName" value="punit"/>
<property name="dataSource" value="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"></property>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDBDialect" />
<entry key="hibernate.hbm2ddl.auto" value="true" />
<entry key="hibernate.format_sql" value="true" />
</map>
</property>
</bean>
Add Transaction Manager and Properties
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Add Data Source
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/fitnessTracker?autoReconnect=true"/>
<property name="username" value="noreal" />
<property name="password" value="noreal" />
</bean>
Annotations
Generally three layers, controller, service and repository with a model
Model=
Models have @Entity
@Entity
@Table(name="Exercises")
public class Exercise {
@Id
@GeneratedValue
@Column(name="EXERCIDE_ID")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}