forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BAEL-2799 * constructor name fix * method name fix * add database * fix tests
- Loading branch information
1 parent
d2102a7
commit 51a9d7b
Showing
6 changed files
with
208 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...istence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.baeldung.jpa.defaultvalues; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
|
||
public class UserRepository { | ||
|
||
private EntityManagerFactory emf = null; | ||
|
||
public UserRepository() { | ||
emf = Persistence.createEntityManagerFactory("entity-default-values"); | ||
} | ||
|
||
public User find(Long id) { | ||
EntityManager entityManager = emf.createEntityManager(); | ||
User user = entityManager.find(User.class, id); | ||
entityManager.close(); | ||
return user; | ||
} | ||
|
||
public void save(User user, Long id) { | ||
user.setId(id); | ||
|
||
EntityManager entityManager = emf.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
entityManager.persist(user); | ||
entityManager.getTransaction().commit(); | ||
entityManager.close(); | ||
} | ||
|
||
public void clean() { | ||
emf.close(); | ||
} | ||
|
||
} |
282 changes: 149 additions & 133 deletions
282
persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,149 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence | ||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" | ||
version="2.2"> | ||
|
||
<persistence-unit name="java-jpa-scheduled-day"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.sqlresultsetmapping.ScheduledDay</class> | ||
<class>com.baeldung.sqlresultsetmapping.Employee</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:database.sql'"/> | ||
<property name="javax.persistence.jdbc.user" value="sa"/> | ||
<property name="javax.persistence.jdbc.password" value=""/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> | ||
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop" />--> | ||
<property name="show_sql" value="true"/> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-h2"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.stringcast.Message</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/> | ||
<property name="javax.persistence.jdbc.user" value="sa"/> | ||
<property name="javax.persistence.jdbc.password" value=""/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop"/> | ||
<property name="show_sql" value="true"/> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-db"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.model.Car</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/baeldung"/> | ||
<property name="javax.persistence.jdbc.user" value="baeldung"/> | ||
<property name="javax.persistence.jdbc.password" value="YourPassword"/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> | ||
<property name="hibernate.show_sql" value="true"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="entity-graph-pu" transaction-type="RESOURCE_LOCAL"> | ||
<class>com.baeldung.jpa.entitygraph.model.Post</class> | ||
<class>com.baeldung.jpa.entitygraph.model.User</class> | ||
<class>com.baeldung.jpa.entitygraph.model.Comment</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
|
||
<!--H2--> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:entitygraphdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/> | ||
|
||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> | ||
<property name="javax.persistence.sql-load-script-source" value="data-init.sql"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="java8-datetime-postgresql" transaction-type="RESOURCE_LOCAL"> | ||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> | ||
<class>com.baeldung.jpa.datetime.JPA22DateTimeEntity</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/java8-datetime2"/> | ||
<property name="javax.persistence.jdbc.user" value="postgres"/> | ||
<property name="javax.persistence.jdbc.password" value="postgres"/> | ||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> | ||
|
||
<!-- configure logging --> | ||
<property name="eclipselink.logging.level" value="INFO"/> | ||
<property name="eclipselink.logging.level.sql" value="FINE"/> | ||
<property name="eclipselink.logging.parameters" value="true"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-h2-criteria"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.criteria.entity.Item</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" | ||
value="org.h2.Driver" /> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:test" /> | ||
<property name="javax.persistence.jdbc.user" value="sa" /> | ||
<property name="javax.persistence.jdbc.password" value="" /> | ||
<property name="hibernate.dialect" | ||
value="org.hibernate.dialect.H2Dialect" /> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> | ||
<property name="show_sql" value="true" /> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" | ||
value="false" /> | ||
<property name="javax.persistence.sql-load-script-source" value="item.sql"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-query-types"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.querytypes.UserEntity</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" | ||
value="org.h2.Driver" /> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:test" /> | ||
<property name="javax.persistence.jdbc.user" value="sa" /> | ||
<property name="javax.persistence.jdbc.password" value="" /> | ||
<property name="hibernate.dialect" | ||
value="org.hibernate.dialect.H2Dialect" /> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> | ||
<property name="show_sql" value="true" /> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" | ||
value="false" /> | ||
<property name="javax.persistence.sql-load-script-source" value="users.sql"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
</persistence> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence | ||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" | ||
version="2.2"> | ||
|
||
<persistence-unit name="java-jpa-scheduled-day"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.sqlresultsetmapping.ScheduledDay</class> | ||
<class>com.baeldung.sqlresultsetmapping.Employee</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:database.sql'"/> | ||
<property name="javax.persistence.jdbc.user" value="sa"/> | ||
<property name="javax.persistence.jdbc.password" value=""/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> | ||
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop" />--> | ||
<property name="show_sql" value="true"/> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-h2"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.stringcast.Message</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/> | ||
<property name="javax.persistence.jdbc.user" value="sa"/> | ||
<property name="javax.persistence.jdbc.password" value=""/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop"/> | ||
<property name="show_sql" value="true"/> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-db"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.model.Car</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/baeldung"/> | ||
<property name="javax.persistence.jdbc.user" value="baeldung"/> | ||
<property name="javax.persistence.jdbc.password" value="YourPassword"/> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> | ||
<property name="hibernate.show_sql" value="true"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="entity-graph-pu" transaction-type="RESOURCE_LOCAL"> | ||
<class>com.baeldung.jpa.entitygraph.model.Post</class> | ||
<class>com.baeldung.jpa.entitygraph.model.User</class> | ||
<class>com.baeldung.jpa.entitygraph.model.Comment</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
|
||
<!--H2--> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:entitygraphdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/> | ||
|
||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> | ||
<property name="javax.persistence.sql-load-script-source" value="data-init.sql"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="java8-datetime-postgresql" transaction-type="RESOURCE_LOCAL"> | ||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> | ||
<class>com.baeldung.jpa.datetime.JPA22DateTimeEntity</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/java8-datetime2"/> | ||
<property name="javax.persistence.jdbc.user" value="postgres"/> | ||
<property name="javax.persistence.jdbc.password" value="postgres"/> | ||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> | ||
|
||
<!-- configure logging --> | ||
<property name="eclipselink.logging.level" value="INFO"/> | ||
<property name="eclipselink.logging.level.sql" value="FINE"/> | ||
<property name="eclipselink.logging.parameters" value="true"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-h2-criteria"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.criteria.entity.Item</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" | ||
value="org.h2.Driver" /> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:test" /> | ||
<property name="javax.persistence.jdbc.user" value="sa" /> | ||
<property name="javax.persistence.jdbc.password" value="" /> | ||
<property name="hibernate.dialect" | ||
value="org.hibernate.dialect.H2Dialect" /> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> | ||
<property name="show_sql" value="true" /> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" | ||
value="false" /> | ||
<property name="javax.persistence.sql-load-script-source" value="item.sql"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="jpa-query-types"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.querytypes.UserEntity</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" | ||
value="org.h2.Driver" /> | ||
<property name="javax.persistence.jdbc.url" | ||
value="jdbc:h2:mem:test" /> | ||
<property name="javax.persistence.jdbc.user" value="sa" /> | ||
<property name="javax.persistence.jdbc.password" value="" /> | ||
<property name="hibernate.dialect" | ||
value="org.hibernate.dialect.H2Dialect" /> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> | ||
<property name="show_sql" value="true" /> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" | ||
value="false" /> | ||
<property name="javax.persistence.sql-load-script-source" value="users.sql"/> | ||
</properties> | ||
</persistence-unit> | ||
|
||
<persistence-unit name="entity-default-values"> | ||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | ||
<class>com.baeldung.jpa.defaultvalues.User</class> | ||
<exclude-unlisted-classes>true</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" /> | ||
<property name="javax.persistence.jdbc.user" value="sa" /> | ||
<property name="javax.persistence.jdbc.password" value="" /> | ||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> | ||
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> | ||
<property name="show_sql" value="true" /> | ||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" /> | ||
</properties> | ||
</persistence-unit> | ||
|
||
</persistence> |
Oops, something went wrong.