Monday, 3 November 2014

Hibernate entity manager and persistence.xml

Hibernate entity manager and persistence.xml 

javax.persistence.jdbc.driver: The JDBC driver class
H2 – org.h2.Driver
Oracle – oracle.jdbc.driver.OracleDriver
MySql – com.mysql.jdbc.Driver

javax.persistence.jdbc.url: The JDBC URL to the database instance.
H2 – jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
Oracle – jdbc:oracle:thin:@127.0.0.1:1521:DBNAME
MySql – jdbc:mysql://localhost/DBNAME

javax.persistence.jdbc.user: The database user name

javax.persistence.jdbc.password: The database user password

Other hibernate properties can be used as required like below
<property name="hibernate.show_sql" value="true" />

Example:
<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_0.xsd"
version="2.0">
<persistence-unit name="com.njoshi.hibernate.employeemanager">
<description>Persistence unit for employee manager</description>

<class>com.njoshi.hibernate.employeemanager.entity.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>

</persistence-unit>
</persistence>

Source: Employee Manager

No comments:

Post a Comment