-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: What Am I Doing Wrong with this One-to-one Relationship?
PostPosted: Thu Jul 19, 2007 8:25 pm 
Newbie

Joined: Mon Jun 12, 2006 1:26 pm
Posts: 3
Location: Minneapolis, Minnesota
Hibernate version: 3.2.1.ga
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using: MySQL 5.0.21
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:

Here are my 2 annotated files: User and UserInfo where UserInfo have a fk which is field in User but not the Primary key. I am confused between the UserInfo.internet_id and UserInfo.user.


Code:
@Entity
@Table(name = "user", catalog = "test", uniqueConstraints = @UniqueConstraint(columnNames = "internet_id"))
public class User extends BaseObject implements java.io.Serializable {
   private static final long serialVersionUID = -1831772440461169308L;
   private String userId;
   private String internetId;
        private UserInfo userInfo;
        .
        .

   @Id
   @Column(name = "user_id", unique = true, nullable = false, length = 64)
   public String getUserId() {
      return this.userId;
   }

   @Column(name = "internet_id", unique = true, nullable = false, length = 32)
   public String getInternetId() {
      return this.internetId;
   }

   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name = "internet_id", insertable = false, updatable = false)
   public UserInfo getUserInfo() {
      return this.userInfo;
   }
.
.
.
}


Code:
@Entity
@Table(name = "user_info", catalog = "test")
public class UserInfo extends BaseObject implements java.io.Serializable {
   private static final long serialVersionUID = 1729408401668444214L;
   private String internetId;
   private User user;
        .
        .

   @Id
   @Column(name = "internet_id", unique = true, nullable = false, length = 64)
   public String getInternetId() {
      return this.internetId;
   }

   @OneToOne (mappedBy="userInfo", optional=true)
   public User getUser() {
      return this.user;
   }
.
.
.
}

Thanks,
Doug


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 9:07 am 
Newbie

Joined: Tue Jun 12, 2007 6:44 am
Posts: 13
Location: Brazil
Iam not sure but Try this:
@OneToOne (mappedBy="user", optional=true)
public User getUser() {
return this.user;
}

Note the mappedBy= property references the other side of the association.

:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 3:18 pm 
Newbie

Joined: Mon Jun 12, 2006 1:26 pm
Posts: 3
Location: Minneapolis, Minnesota
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. :-(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.