| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| hi, I used 1.1.0 to try to implement a simple JOINED inhertance using orm.xml: <entity class="Parent"> <table name="Parent"/> <inheritance strategy="JOINED"/> <attributes> <id name="partyID"> <column name="PARTYID" column-definition="VARCHAR(200)" nullable="false"/> </id> ... </attributes> </entity> <entity class="Child"> <table name="Child"/> <primary-key-join-column name="PARTYID" referenced-column-name="PARTYID"/> <attributes> <basic name="fullRegisteredName"> <column name="FULLREGISTEREDNAME" column-definition="VARCHAR(200)" updatable="true" insertable="true"/> </basic> ..... when I try to persist multiple Child objects, the first one would be ok but the following ones will fail and from trace, it seems that it keeps inserting into the "Child" table 2 times instead of being inserting Parent table first then Child table. It looks no problem if I run it under 1.0.2 version. Is it a bug? Thx Acton |
|
#2
| |||
| |||
| I too get the exact same problem. In fact, it behaves quite wierdly in my case. I have two methods doing the exact same thing i.e insert a Creditor object with different ID's. With the object hierarchy below, I expect it to fire a Customer query insert and then a creditor query insert for both the methods. However, openJPA behaves randomly. It sometimes does it right for one method, sometimes for the other, sometimes none at all. One thing that's consistent is that whenever i try and insert an object with the ID already in the database, it fires the wrong inserts and throws an error. Also, I'm using the exact same version you mentioned i.e 1.1.0 Here is my test code public class BaseDAOTest { public void testCreateInheritance() { System.out.println("testInheritacne"); EntityManager manager = null; Creditor creditor = new Creditor(new Double(1004), new Double(659), "DK3", new Date(), new Date(), new Date(), "PROG", new Date()); try { //Invocations to the processing logic manager = FactoryTest.factory.createEntityManager(); EntityTransaction transaction = manager.getTransaction(); transaction.begin(); BaseDAO.create(manager, creditor); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { //Do this instead of closing the manager directly on your own. manager.close(); } } public void testCreateManyToOne() { System.out.println("manytoone"); EntityManager manager = null; Creditor creditor = new Creditor(new Double(1005), new Double(126), "DK3", new Date(), new Date(), new Date(), "PROG", new Date()); try { //Invocations to the processing logic manager = FactoryTest.factory.createEntityManager(); EntityTransaction transaction = manager.getTransaction(); transaction.begin(); BaseDAO.create(manager, creditor); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { //Do this instead of closing the manager directly on your own. //PersistenceBootstrapper.closeManager(); manager.close(); } } public static void main(String[] args) { BaseDAOTest test = new BaseDAOTest(); test.testCreateInheritance(); test.testCreateManyToOne(); } } My object graph is as follows... @Entity @Table(name="Customer") @Inheritance(strategy=InheritanceType.JOINED) public class Customer implements Serializable { @Id @Column(name="PBS_NO", nullable=false,length=8) public Double pbsNo; @Column(name="CVR_NO", nullable=false,length=8) private Double cvrNo; public Customer() { } public Customer(Double pbsNo, Double cvrNo, String postCode, Date startDate, Date valStartDate, Date valEndDate, String updateBy, Date updateTS) { this.pbsNo = pbsNo; this.cvrNo = cvrNo; this.postCode = postCode; this.startDate = startDate; this.valStartDate = valStartDate; this.valEndDate = valEndDate; this.updateBy = updateBy; this.updateTS = updateTS; } <<snip>> } @Entity @Table(name="CREDITOR") @PrimaryKeyJoinColumn(name="PBS_NO", referencedColumnName="PBS_NO") public class Creditor extends Customer implements Serializable { @Column(name="CRED_TYPE", nullable=false) private String credType; public Creditor() { } public Creditor(Double pbsNo, Double cvrNo, String postCode, Date startDate, Date valStartDate, Date valEndDate, String updateBy, Date updateTS) { super(pbsNo, cvrNo, postCode, startDate, valStartDate, valEndDate, updateBy, updateTS); this.credType = "Premium"; this.billType = "MTHL"; this.startDate = new Date(); this.valStartDate = new Date(); this.valEndDate = new Date(); this.updateBy = "PROG"; this.updateTS = new Date(); } <<snip>> } Acton Wang wrote: > > > hi, > > I used 1.1.0 to try to implement a simple JOINED inhertance using > orm.xml: > > > <entity class="Parent"> <table name="Parent"/> <inheritance > strategy="JOINED"/> <attributes> <id name="partyID"> <column > name="PARTYID" column-definition="VARCHAR(200)" nullable="false"/> </id> > ... </attributes> </entity> <entity class="Child"> <table > name="Child"/> <primary-key-join-column name="PARTYID" > referenced-column-name="PARTYID"/> <attributes> <basic > name="fullRegisteredName"> <column name="FULLREGISTEREDNAME" > column-definition="VARCHAR(200)" updatable="true" insertable="true"/> > </basic> ..... > > when I try to persist multiple Child objects, the first one would be > ok but the following ones will fail and from trace, it seems that it keeps > inserting into the "Child" table 2 times instead of being inserting Parent > table first then Child table. > > It looks no problem if I run it under 1.0.2 version. Is it a bug? > > Thx > Acton > -- View this message in context: http://n2.nabble.com/problem-when-pe...91p781339.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#3
| |||
| |||
| Hi, in my DAO i am injecting via Spring: @PersistenceContext private EntityManager em; public void save(WSO wso) { OpenJPAEntityManager oem = (OpenJPAEntityManager) em; if (oem.isDetached(wso)) { em.merge(wso); } else { em.persist(wso); } } But the "em" is not compatible with OpenJPAEntityManager, so i get a ClassCastException. My boostrapping code via Spring looks like this: <bean id="entityManagerFactory" class ="org.springframework.orm.jpa.LocalContainerEntity ManagerFactoryBean"> <property name="dataSource" ref="myDataSource"/> <!-- <property name="loadTimeWeaver"> <bean class = "org.springframework.instrument.classloading.Refle ctiveLoadTimeWeaver"/> </property> --> </bean> And my 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_1_0.xsd" version="1.0"> <persistence-unit name="punit"> <provider>org.apache.openjpa.persistence.Persisten ceProviderImpl</ provider> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> <property name="openjpa.DetachState" value="fetch- groups(DetachedStateField=true)"/> </properties> </persistence-unit> </persistence> So how can i use methods only available in OpenJPAEntityManager? Its somehow ugly anyway to ask if its detached and then do a merge or persist. I couldnt remember that this was the case with Kodo. Thx for ideas. Marc |
|
#4
| |||
| |||
| Hi, Instead of OpenJPAEntityManager oem = (OpenJPAEntityManager) em; try OpenJPAEntityManager oem = OpenJPAPersistence.cast(em); -- View this message in context: http://n2.nabble.com/problem-when-pe...91p781619.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#5
| |||
| |||
| Subsequent to my last post, I have tried the code on versions 1.0.2 and 1.2.0. It fails on both ( in addition to 1.1.0 on which I originally tried it). Another observation is that if I'm persisting a base class subclass hierarchy twice in two methods, like I'm doing, the first invocation fails. However openJPA now understands the inheritance hierarchy properly and the subsequent call succeeds with openJPA firing the inserts in the proper order. This seemingly trivial piece of code is giving me a lot of strife not to mention the fact that I went on a wild goose chase thinking the problem lay somewhere else. Can someone please help me out here ?? I have attached my SQL trace log which should give some pointers. Is Inheritancetype.JOINED working for anyone with any version. Can someone please verify and tell me what I'm doing wrong ? http://n2.nabble.com/file/n781623/openjpa.log openjpa.log Acton Wang wrote: > > > hi, > > I used 1.1.0 to try to implement a simple JOINED inhertance using > orm.xml: > > > <entity class="Parent"> <table name="Parent"/> <inheritance > strategy="JOINED"/> <attributes> <id name="partyID"> <column > name="PARTYID" column-definition="VARCHAR(200)" nullable="false"/> </id> > ... </attributes> </entity> <entity class="Child"> <table > name="Child"/> <primary-key-join-column name="PARTYID" > referenced-column-name="PARTYID"/> <attributes> <basic > name="fullRegisteredName"> <column name="FULLREGISTEREDNAME" > column-definition="VARCHAR(200)" updatable="true" insertable="true"/> > </basic> ..... > > when I try to persist multiple Child objects, the first one would be > ok but the following ones will fail and from trace, it seems that it keeps > inserting into the "Child" table 2 times instead of being inserting Parent > table first then Child table. > > It looks no problem if I run it under 1.0.2 version. Is it a bug? > > Thx > Acton > -- View this message in context: http://n2.nabble.com/problem-when-pe...91p781623.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#6
| |||
| |||
| Another observation is that if I'm persisting a base class subclass hierarchy twice in two methods, like I'm doing, the first invocation fails. However openJPA now understands the inheritance hierarchy properly and the subsequent call succeeds with openJPA firing the inserts in the proper order. I stand corrected. The above observation is incorrect as I have today discovered that the behaviour is well and truly random. Sometimes openJPA fires queries in the correct order, sometimes it fires queries for the child class twice instead of parent class first and child class later. I also tried using an orm.xml (instead of annotations) without any success. Here is my database schema... create table <SCHEMA>.CUSTOMER ( PBS_NO DECIMAL(8) not null, CVR_NO DECIMAL(8) not null, SP_NO DECIMAL(10), BP_NO DECIMAL(10), BP_NO_OWNER CHAR(4), HREG_NO DECIMAL(4), PREFIX CHAR(10), NAME1 VARCHAR(70), NAME2 VARCHAR(70), ADDR1 VARCHAR(70), ADDR2 VARCHAR(70), ADDR3 VARCHAR(70), POSTAL_CD CHAR(10) not null, CITY VARCHAR(70), COUNTRY_CD CHAR(2), LANG_CD CHAR(4), START_DT DATE not null, VAL_START_DT DATE not null, VAL_END_DT DATE not null, UPDATE_BY CHAR(10) not null, UPDATE_TS TIMESTAMP not null, constraint PK_CU primary key (PBS_NO) ); create table <SCHEMA>.CREDITOR ( PBS_NO DECIMAL(8) not null, CRED_TYPE VARCHAR(20) not null, BILLING_TYPE CHAR(4) not null, PREMIUM_CUST_FLG CHAR(1), START_DT DATE not null, VAL_START_DT DATE not null, VAL_END_DT DATE not null, UPDATE_BY CHAR(10) not null, UPDATE_TS TIMESTAMP not null, constraint PK_CR primary key (PBS_NO) ); alter table <SCHEMA>.CREDITOR add constraint F_FK_CU foreign key (PBS_NO) references <SCHEMA>.CUSTOMER (PBS_NO) on delete no action; devu213 wrote: > > Subsequent to my last post, I have tried the code on versions 1.0.2 and > 1.2.0. It fails on both ( in addition to 1.1.0 on which I originally tried > it). > > Another observation is that if I'm persisting a base class subclass > hierarchy twice in two methods, like I'm doing, the first invocation > fails. However openJPA now understands the inheritance hierarchy properly > and the subsequent call succeeds with openJPA firing the inserts in the > proper order. > > This seemingly trivial piece of code is giving me a lot of strife not to > mention the fact that I went on a wild goose chase thinking the problem > lay somewhere else. > > Can someone please help me out here ?? I have attached my SQL trace log > which should give some pointers. > > Is Inheritancetype.JOINED working for anyone with any version. Can someone > please verify and tell me what I'm doing wrong ? > http://n2.nabble.com/file/n781623/openjpa.log openjpa.log > > > > > Acton Wang wrote: >> >> >> hi, >> >> I used 1.1.0 to try to implement a simple JOINED inhertance using >> orm.xml: >> >> >> <entity class="Parent"> <table name="Parent"/> <inheritance >> strategy="JOINED"/> <attributes> <id name="partyID"> <column >> name="PARTYID" column-definition="VARCHAR(200)" nullable="false"/> >> </id> >> ... </attributes> </entity> <entity class="Child"> <table >> name="Child"/> <primary-key-join-column name="PARTYID" >> referenced-column-name="PARTYID"/> <attributes> <basic >> name="fullRegisteredName"> <column name="FULLREGISTEREDNAME" >> column-definition="VARCHAR(200)" updatable="true" insertable="true"/> >> </basic> ..... >> >> when I try to persist multiple Child objects, the first one would be >> ok but the following ones will fail and from trace, it seems that it >> keeps inserting into the "Child" table 2 times instead of being inserting >> Parent table first then Child table. >> >> It looks no problem if I run it under 1.0.2 version. Is it a bug? >> >> Thx >> Acton >> > > -- View this message in context: http://n2.nabble.com/problem-when-pe...91p783275.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#7
| |||
| |||
| For whatever it's worth, I'm using DB2 Express C as the database. Can someone please confirm that Inheritance JOINED works? devu213 wrote: > > > Another observation is that if I'm persisting a base class subclass > hierarchy twice in two methods, like I'm doing, the first invocation > fails. However openJPA now understands the inheritance hierarchy properly > and the subsequent call succeeds with openJPA firing the inserts in the > proper order. > > I stand corrected. The above observation is incorrect as I have today > discovered that the behaviour is well and truly random. Sometimes openJPA > fires queries in the correct order, sometimes it fires queries for the > child class twice instead of parent class first and child class later. > > I also tried using an orm.xml (instead of annotations) without any > success. > > Here is my database schema... > > > create table <SCHEMA>.CUSTOMER > ( > PBS_NO DECIMAL(8) not null, > CVR_NO DECIMAL(8) not null, > SP_NO DECIMAL(10), > BP_NO DECIMAL(10), > BP_NO_OWNER CHAR(4), > HREG_NO DECIMAL(4), > PREFIX CHAR(10), > NAME1 VARCHAR(70), > NAME2 VARCHAR(70), > ADDR1 VARCHAR(70), > ADDR2 VARCHAR(70), > ADDR3 VARCHAR(70), > POSTAL_CD CHAR(10) not null, > CITY VARCHAR(70), > COUNTRY_CD CHAR(2), > LANG_CD CHAR(4), > START_DT DATE not null, > VAL_START_DT DATE not null, > VAL_END_DT DATE not null, > UPDATE_BY CHAR(10) not null, > UPDATE_TS TIMESTAMP not null, > constraint PK_CU primary key (PBS_NO) > ); > > > create table <SCHEMA>.CREDITOR > ( > PBS_NO DECIMAL(8) not null, > CRED_TYPE VARCHAR(20) not null, > BILLING_TYPE CHAR(4) not null, > PREMIUM_CUST_FLG CHAR(1), > START_DT DATE not null, > VAL_START_DT DATE not null, > VAL_END_DT DATE not null, > UPDATE_BY CHAR(10) not null, > UPDATE_TS TIMESTAMP not null, > constraint PK_CR primary key (PBS_NO) > ); > alter table <SCHEMA>.CREDITOR > add constraint F_FK_CU foreign key (PBS_NO) > references <SCHEMA>.CUSTOMER (PBS_NO) > on delete no action; > > > > devu213 wrote: >> >> Subsequent to my last post, I have tried the code on versions 1.0.2 and >> 1.2.0. It fails on both ( in addition to 1.1.0 on which I originally >> tried it). >> >> Another observation is that if I'm persisting a base class subclass >> hierarchy twice in two methods, like I'm doing, the first invocation >> fails. However openJPA now understands the inheritance hierarchy properly >> and the subsequent call succeeds with openJPA firing the inserts in the >> proper order. >> >> This seemingly trivial piece of code is giving me a lot of strife not to >> mention the fact that I went on a wild goose chase thinking the problem >> lay somewhere else. >> >> Can someone please help me out here ?? I have attached my SQL trace log >> which should give some pointers. >> >> Is Inheritancetype.JOINED working for anyone with any version. Can >> someone please verify and tell me what I'm doing wrong ? >> http://n2.nabble.com/file/n781623/openjpa.log openjpa.log >> >> >> >> >> Acton Wang wrote: >>> >>> >>> hi, >>> >>> I used 1.1.0 to try to implement a simple JOINED inhertance using >>> orm.xml: >>> >>> >>> <entity class="Parent"> <table name="Parent"/> <inheritance >>> strategy="JOINED"/> <attributes> <id name="partyID"> <column >>> name="PARTYID" column-definition="VARCHAR(200)" nullable="false"/> >>> </id> >>> ... </attributes> </entity> <entity class="Child"> <table >>> name="Child"/> <primary-key-join-column name="PARTYID" >>> referenced-column-name="PARTYID"/> <attributes> <basic >>> name="fullRegisteredName"> <column name="FULLREGISTEREDNAME" >>> column-definition="VARCHAR(200)" updatable="true" insertable="true"/> >>> </basic> ..... >>> >>> when I try to persist multiple Child objects, the first one would be >>> ok but the following ones will fail and from trace, it seems that it >>> keeps inserting into the "Child" table 2 times instead of being >>> inserting Parent table first then Child table. >>> >>> It looks no problem if I run it under 1.0.2 version. Is it a bug? >>> >>> Thx >>> Acton >>> >> >> > > -- View this message in context: http://n2.nabble.com/problem-when-pe...91p785849.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#8
| |||
| |||
| > Can someone please confirm that Inheritance JOINED works? Yes. Many of the insert statements for CREDITOR in the log shows PBS_NO 668.0. No wonder DB2 is complaining about SQL ERROR 23505 which is violation of database unique constraint as PBS_NO happens to be the primary key. What are the persistence identity value generation strategies employed by Customer and Creditor class? -- View this message in context: http://n2.nabble.com/problem-when-pe...91p786275.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#9
| |||
| |||
| Hi, I'm sorry, I looked at the log file that I attached and realized that it has gotten truncated due to some character in between (I copy pasted it from eclipse console). Here is the entire log file on a different run. Pinaki, your observation is correct but the thing is the very reason that the constraint gets violated is because instead of generating the Customer insert and the Creditor insert, the framework generates two creditor inserts (for any ID - 648 in this case) leading to key violations on the database. If it were firing the proper inserts, this wouldn't be the case. In the attached log you'll see that the first set of inserts is correct (ID 1217). However for the second set of inserts, it fires the Creditor insert query twice as opposed to Customer and then Creditor (ID 1216) and this leads to the violation. I find no visible reason for this to happen. Here is my calling code. package dk.pbs.bs.dao.unittests; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; public class BaseDAOTest { public void testCreateInheritance() { System.out.println("testInheritacne"); EntityManager manager = null; Creditor creditor = new Creditor(new Double(1216), new Double(659), "DK3", new Date(), new Date(), new Date(), "PROG", new Date()); try { //Invocations to the processing logic manager = FactoryTest.factory.createEntityManager(); EntityTransaction transaction = manager.getTransaction(); transaction.begin(); BaseDAO.create(manager, creditor); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { manager.close(); } } public void testCreateManyToOne() { System.out.println("manytoone"); EntityManager manager = null; Creditor creditor = new Creditor(new Double(1217), new Double(126), "DK3", new Date(), new Date(), new Date(), "PROG", new Date()); try { //Invocations to the processing logic manager = FactoryTest.factory.createEntityManager(); EntityTransaction transaction = manager.getTransaction(); transaction.begin(); BaseDAO.create(manager, creditor); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { manager.close(); } } public static void main(String[] args) { BaseDAOTest test = new BaseDAOTest(); test.testCreateManyToOne(); test.testCreateInheritance(); } } The identity generation strategy at this point is "assigned"(to borrow a hibernate term, haven't looked up the equivalent openJPA term). So at this point, I'm just hard coding unique ID's. Will change this in the future once I get a handle on all kinds of relationships working properly. HTH... Hope to hear from someone soon. Pinaki Poddar wrote: > >> Can someone please confirm that Inheritance JOINED works? > Yes. > > Many of the insert statements for CREDITOR in the log shows PBS_NO 668.0. > No wonder DB2 is complaining about SQL ERROR 23505 which is violation of > database unique constraint as PBS_NO happens to be the primary key. > > What are the persistence identity value generation strategies employed by > Customer and Creditor class? > > > > > > http://n2.nabble.com/file/n788679/updatedlog.log updatedlog.log -- View this message in context: http://n2.nabble.com/problem-when-pe...91p788679.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
|
#10
| |||
| |||
| Hi, Please post a reproducible JUnit (not DBUnit) test with all required artifacts (entities, mapping annotations/descriptors, persistence.xml) with assertions to demonstrate the exact nature of failure. Then we can confirm whether the reported observation is a OpenJPA limitation/bug or not. -- View this message in context: http://n2.nabble.com/problem-when-pe...91p789889.html Sent from the OpenJPA Users mailing list archive at Nabble.com. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.