I m using an osgi application with postgresql connection and a persistence.xml that containt some properties like
<persistence-unit name="postgres-name"
transaction-type="JTA">
<properties>
<!-- <property name="show_sql" value="true" /> -->
<!-- <property name="hibernate.format_sql" value="true" /> -->
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
...many other properties...
</properties>
</persistence-unit>
And in another bundle i want to initialize a H2database with its own unitname, so i have declared a new persistence.xml with :
<persistence-unit name="h2-unitname"
transaction-type="RESOURCE_LOCAL">
<properties>
...many properties...
<property name="eclipselink.allow-zero-id" value="true"/>
...many other properties...
Problem is when i get EntityManager for postgres database, all work fine, next i get h2 EntityManager and work fine, but next time postgres EntityManager is used, i have an error :
Detail: key (id)=(0) already exist
Call: INSERT INTO TABLE(ID, TEST) VALUES (?, ?)
bind => [0, null]
I think my property eclipselink.allow-zero-id define in h2 unitname ovveride the postgres EntityManager...
In fact if i use always postgres database with 0 id, all wark fine, because sequence is call, but if i use one time the h2 EntityManager, entity with 0 id don't work anymore because of allow-zero-id.
How can i define separate property for each persistent unitname?
Ask me if you want to see more configuration
thanks by advance
Related
This is the situation. I have a table called customer and this table is repeating among different schemes in the same database. so the problem is when i'm going to map this customer table with hibernate entity do i need to create multiple classes for specifying the schema or can i dynamically change the schema in the entity. I'm a newbie to this hibernate framework please help.
You need to have more persistence unit. And use entity manager created via entity manager factory which used proper persistence unit.
In persistence.xml
<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.connection.url" value="jdbc:database://url1"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="pass"/>
...
</persistence-unit>
<persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.connection.url" value="jdbc:database://url12"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="pass"/>
...
</persistence-unit>
private static EntityManagerFactory emf1;
private static EntityManagerFactory emf2;
emf1 = Persistence.createEntityManagerFactory("pu1");
emf2 = Persistence.createEntityManagerFactory("pu2");
emf1.createEntityManager();//Will store to database defined in pu1
emf2.createEntityManager();//Will store to database defined in pu2
I am using JPA with Hibernate implementation and i have created a datasource in websphere server. I am trying to access this datasource in my JPA application. But, when i try to access , i am getting the below exception . Please help me out if you have come across such situation. Thanks
org.hibernate.HibernateException: Could not find datasource
Caused by:
javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
Please find the below details:
Datasource Name: pieportaldev_0
JNDI Name: pie/logDB
And my Persistance.xml is
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- the JNDI data source -->
<jta-data-source>java:comp/env/jdbc/pieportaldev_0</jta-data-source>
<class>com.test.jpa.UserInfo</class>
<properties>
<!-- if this is true, hibernate will print (to stdout) the SQL it executes, so you can check it to ensure it's not doing anything crazy -->
<!-- <property name="hibernate.show_sql" value="true" /> -->
<property name="hibernate.format_sql" value="true" />
<!-- since most database servers have slightly different versions of the SQL, Hibernate needs you to choose a dialect so it knows the subtleties of talking
to that server -->
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<!-- this tell Hibernate to update the DDL when it starts, very useful for development, dangerous in production -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
Hibernate is able to create database structure. Below is example how configuration looks using JPA2 with <property name="hibernate.hbm2ddl.auto" value="create"/>
( Use Hibernate Entity Manager in the case)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
</persistence>
How to make Hibernate to create database SQL script for initialization and update? (that could be run manually or via batch execution) and what tools to use for that.
Yes, there are SQL statements in log (Q Getting SQL script from Hibernate update ), but that is not clean solution.
The goal is to prepare solution for automated database upgrade with every version release.
For automated database upgrades, look at another tool specifically designed for the job. I would suggest Liqiubase.
I have one java project. In that I use #Entity. In my project have some constraints. Tables should be created automatically. So I use JPa #Entity. what the problem is that, when ever the Program is restarting, the new table is created by the Entity. So my old data got losed. I need those type of data. Trap Entity should not overwrite my content in Database? How to solve it?
use <property name="hibernate.hbm2ddl.auto" value="update"/> in your persistence.xml . If you have not generate the schema then first use <property name="hibernate.hbm2ddl.auto" value="create"/> and the schema will be generated. Then use <property name="hibernate.hbm2ddl.auto" value="update"/> since now you have the schema.
In the persistence.xml file there should be a setting to turn off the auto generation of DLL. Here is an example of a persistence.xml file using hibernate where the hibernate.hbm2ddl.auto property has been set to validate, which will cause the tables not to be overwritten. If your relying on Hibernate to create your DDL you would not want to have this set the first time your run the application, it would be set after running once and successfully creating the DDL (Tables).
If this setting were set to create-drop the schema would recreate the tables each time the persistence context is created, causing all data to be lost. This property will be specific to your ORM vendor's implementation.
The best advice is to find the documentation for your vendor's specific setting and then choose the option which best fits your needs. This post does a good job of describing the possible values for the setting in Hibernate.
<persistence-unit name="toThought" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
In EclipseLink this property is:
<property name="eclipselink.ddl-generation" value="create-tables"/>
I have a persistence unit like this:
<persistence-unit name="persistence Unit"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.User</class>
<class>com.example.Team</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<!-- JTA stuff -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.BTMTransactionManagerLookup" />
</properties>
</persistence-unit>
When the server loads first time, this creates table for User and Team, but i dont want to create a table for Team. Is it possible?
Some settings in the persistence table maybe?
If table Team isn't changed (INSERT or UPDATE) by your application in any way, you could remove write permissions for it in your database system.
Exclude Team class from your persistance.xml Remove following line.
<class>com.example.Team</class>