charllescuba,
So I did some shifting around, according to the Annotation document, the Entity that have the "fk" should have the following annotation:
Table UserInfo have the foreign key of "internet_id" from the User table:
@Entity
public class UserInfo implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="internet_id")
public User getUser() {
...
}
and the other class User
@Entity
public class User implements Serializable {
@OneToOne(mappedBy = "user")
public UserInfo getUserInfo() {
...
}
I also made each tables to have its own @Id field just to see if it's any better. Because the tables are one-to-one, the 2nd table should not need an Identity field, the foreign key should be sufficient. Anyway, everything seems to compile and no configuration problem when the following test yield an error.
Code:
.
.
.
public void testCRUD() {
User user = new User();
// set required fields
user.setUniversityId("2222222");
user.setInternetId("tester001");
user.setFirstName("first");
user.setLastName("last");
UserInfo userInfo = new UserInfo();
userInfo.setUser(user);
userInfo.setEmail("tester001@mydomain.com");
userInfo.setContractYear("2007");
userInfo.setStaffType("FULL_TIME");
user.setUserInfo(userInfo);
// create
user = (User)universalDao.save(user);
flush();
assertNotNull(user.getUserId());
.
.
.
}
Quote:
testCRUD(myApp.dao.UniversalDaoTest) Time elapsed: 2.657 sec <<< ERROR!
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: myApp.model.User; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: myApp.model.User
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: myApp.model.User
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:410)
at org.hibernate.type.EntityType.replace(EntityType.java:253)
at org.hibernate.type.AbstractType.replace(AbstractType.java:153)
at org.hibernate.type.TypeFactory.replaceAssociations(TypeFactory.java:530)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:366)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:195)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
at org.springframework.orm.hibernate3.HibernateTemplate$23.doInHibernate(HibernateTemplate.java:765)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:762)
at myApp.dao.hibernate.UniversalDaoHibernate.save(UniversalDaoHibernate.java:27)
at myApp.dao.UniversalDaoTest.testCRUD(UniversalDaoTest.java:45)
at myApp.dao.UniversalDaoTest.testCRUD(UniversalDaoTest.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:138)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:125)
at org.apache.maven.surefire.Surefire.run(Surefire.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:290)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:818)
Thanks,
Doug
PS: This should be simple! User have a one-to-one record which is UserInfo. UnserInfo have a foreign key which is one of the unique field in User. :-(