Hi all,
I have two persistence classes A and B having one to one relation. I got
these generated using ReverseMappingtool from mysql database where i created
A and B tables before hand by using the create statements below. I got the
orm.xml also with that. A has foreignkey relationship in table B in the
mysql DB.
CREATE TABLE `test`.`a` (
`AUTID` int(10) unsigned NOT NULL auto_increment,
`USERNM` char(20) NOT NULL,
`PSWD` char(18) NOT NULL,
`TS` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
PRIMARY KEY (`AUTID`)
) ENGINE=InnoDB AUTO_INCREMENT=111 DEFAULT CHARSET=latin1;
CREATE TABLE `test`.`b` (
`AUTID` int(10) unsigned NOT NULL auto_increment,
`CLNTEMAILADDR` char(72) NOT NULL,
`TS` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
PRIMARY KEY (`AUTID`),
CONSTRAINT `FK_email_1` FOREIGN KEY (`AUTID`) REFERENCES `crdntl`
(`AUTID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=latin1;
part of orm.xml is below
<entity class="com.test.A">
<table name="A"/>
<attributes>
<id name="autUid">
<column name="AUTID"/>
<generated-value strategy="IDENTITY" />
</id>
<one-to-one name="b" target-entity="com.test.B" fetch="LAZY"
mapped-by="a">
<cascade>
<cascade-all/>
</cascade>
</one-to-one>
</attributes>
</entity>
<entity class="com.test.B">
<table name="B"/>
<attributes>
<id name="autUid">
<column name="AUTID"/>
<generated-value strategy="IDENTITY" />
</id>
<one-to-one name="a">
<join-column name="AUTID" referenced-column-name="AUTID" />
<cascade>
<cascade-all/>
</cascade>
</one-to-one>
</attributes>
</entity>
I am trying to do as below in my main class.
em.getTransaction().begin();
A a= new A();
a.setPass("testpass");
a.setUsername("testname");
B b= new B();
b.setClntEmailaddr("[email protected]");
b.setTS(new Date());
a.setB(b);
a.setTs(new Date());
em.persist(a);
em.getTransaction().commit();
I am getting the following error:
org.apache.openjpa.lib.jdbc.ReportingSQLException: Cannot add or update a
child row: a foreign key constraint fails (`test/b`, CONSTRAINT `FK_email_1`
FOREIGN KEY (`AUTID`) REFERENCES `a` (`AUTID`) ON DELETE CASCADE) {prepstmnt
1202603950 INSERT INTO testjpa.email (AUTID, CLNTEMAILADDR, TS) VALUES (?,
?, ?) [params=(int) 0, (String) [email protected], (Timestamp) 2010-12-29
10:36:39.787]} [code=1452, state=23000]
If I add this line b.setA(a);em.persist(a); in the above code, then i am
getting the following error
<openjpa-2.0.1-r422266:989424 fatal user error>
org.apache.openjpa.persistence.InvalidStateException: Attempt to set column
"b.AUTID" to two different values: (class java.lang.Integer)"0", (class
java.lang.Integer)"5" This can occur when you fail to set both sides of a
two-sided relation between objects, or when you map different fields to the
same column, but you do not keep the values of these fields in synch.
How do i resolve.
Thanks,
-Venra
--
View this message in context:
http://openjpa.208410.n2.nabble.com/Having-problem-with-existing-database-tables-with-generated-orm-xml-and-persitence-classes-using-Revl-tp5874988p5874988.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.