-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: one-to-one to be bidirectional or not
PostPosted: Wed Oct 08, 2003 5:51 pm 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
I am having problems with a fairly straight forward one-one mapping. I have a Vendor (parent) that has a one-to-one mapping with Contact (chils).

The Contact table has a FK constraint on the Vendor table.

I am getting a FK constrained violation primarily because HB is trying to save Contact instead of saving Vendor first and then Contact.

Please advise.

Below are the Vendor and Contact mappings respectively:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.demo.hb.Vendor" table="HB_VENDOR">
        <id name="vendorId" column="VENDORID">
           <generator class="foreign">
            <param name="property">vendorContact</param>
           </generator>
        </id>
        <property name="vendorName" column="VENDORNAME"/>
        <one-to-one name="vendorContact" class="com.demo.hb.Contact" cascade="all" constrained="true"/>
    </class>
</hibernate-mapping>

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.demo.hb.Contact" table="HB_VENDOR_CONTACT">
       <id name="contactId" column="VENDORID" unsaved-value="any">
           <generator class="sequence">
              <param name="sequence">seq_vendorid</param>
           </generator>
          </id>
        <property name="contactCity" column="CITY"/>
        <property name="contactCountry" column="COUNTRY"/>
        <property name="contactFirstName" column="CONTACTFIRSTNAME"/>
        <property name="contactLastName" column="CONTACTLASTNAME"/>
        <property name="contactLine1" column="ADDRESSLINE1"/>
        <property name="contactLine2" column="ADDRESSLINE2"/>
        <property name="contactState" column="STATE"/>
        <property name="contactTitle" column="CONTACTTITLE"/>
        <property name="contactZip" column="ZIP"/>
      <one-to-one name="contactVendor" class="com.demo.hb.Vendor" cascade="all" constrained="true"/>
    </class>
</hibernate-mapping>

Also here is the stack trace with logging turned on:
Code:
17:42:55,304 DEBUG SessionFactoryImpl:281 - instantiated session factory
17:42:55,314 DEBUG SessionImpl:413 - opened session
17:42:55,324 DEBUG BatcherImpl:166 - about to open: 0 open PreparedStatements, 0 open ResultSets
17:42:55,324 DEBUG DriverManagerConnectionProvider:77 - total checked-out connections: 0
17:42:55,324 DEBUG DriverManagerConnectionProvider:83 - using pooled JDBC connection, pool size: 0
17:42:55,324 DEBUG SessionFactoryImpl:526 - prepared statement get: select seq_vendorid.nextval from dual
17:42:55,334 DEBUG SessionFactoryImpl:536 - preparing statement
17:42:55,545 DEBUG SequenceGenerator:70 - Sequence identifier generated: 100020
17:42:55,545 DEBUG BatcherImpl:173 - done closing: 0 open PreparedStatements, 0 open ResultSets
17:42:55,545 DEBUG SessionFactoryImpl:554 - closing statement
17:42:55,555 DEBUG SessionImpl:656 - saving [com.demo.hb.Contact#100020]
17:42:55,555 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Contact
17:42:55,565 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Contact
17:42:55,565 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Contact
17:42:55,565 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Contact
17:42:55,575 DEBUG SessionImpl:656 - saving [com.demo.hb.Vendor#100020]
17:42:55,575 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG Cascades:87 - cascading to saveOrUpdate()
17:42:55,575 DEBUG SessionImpl:1183 - saveOrUpdate() persistent instance
17:42:55,575 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG SessionImpl:2011 - flushing session
17:42:55,575 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Contact
17:42:55,585 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Contact
17:42:55,585 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
17:42:55,585 DEBUG Cascades:87 - cascading to saveOrUpdate()
17:42:55,585 DEBUG SessionImpl:1183 - saveOrUpdate() persistent instance
17:42:55,585 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
17:42:55,585 DEBUG SessionImpl:2113 - Flushing entities and processing referenced collections
17:42:55,585 DEBUG SessionImpl:2397 - Processing unreferenced collections
17:42:55,595 DEBUG SessionImpl:2408 - Scheduling collection removes/(re)creates/updates
17:42:55,595 DEBUG SessionImpl:2023 - Flushed: 2 insertions, 0 updates, 0 deletions to 2 objects
17:42:55,595 DEBUG SessionImpl:2028 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
17:42:55,595 DEBUG SessionImpl:2058 - executing flush
17:42:55,595 DEBUG EntityPersister:464 - Inserting entity: com.demo.hb.Contact#100020
17:42:55,595 DEBUG BatcherImpl:166 - about to open: 0 open PreparedStatements, 0 open ResultSets
17:42:55,595 DEBUG SessionFactoryImpl:526 - prepared statement get: insert into HB_VENDOR_CONTACT (CITY, COUNTRY, CONTACTFIRSTNAME, CONTACTLASTNAME, ADDRESSLINE1, ADDRESSLINE2, STATE, CONTACTTITLE, ZIP, VENDORID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
17:42:55,595 DEBUG SessionFactoryImpl:536 - preparing statement
17:42:55,595 DEBUG EntityPersister:366 - Dehydrating entity: com.demo.hb.Contact#100020
17:42:55,605 DEBUG BatcherImpl:24 - Adding to batch
17:42:55,605 DEBUG EntityPersister:464 - Inserting entity: com.demo.hb.Vendor#100020
17:42:55,605 DEBUG BatcherImpl:46 - Executing batch size: 1
17:42:55,615 DEBUG JDBCExceptionReporter:36 - SQL Exception
java.sql.BatchUpdateException: ORA-02291: integrity constraint (EP10.HB_VENDOR_CONTACT_FK) violated - parent key not found

        at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
        at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4133)
        at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:50)
        at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:105)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 1:10 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Ok. From the mapping files, you are telling Hibernate that CONTACT is your parent, and that VENDOR is your "child". Reason is, the primary key generation is in CONTACT, while the foreign generation is in VENDOR. I think this is the inverse of what you want.

If VENDOR is your primary table, then you must make it contain the PK generator and have CONTACT contain the FK generator.

It may be, that in your DB, you've defined the one-to-one as having the VENDOR table as the primary table, but in your mapping, you've made CONTACT the primary table, I think that's where the conflict may be. In my mapping, I leave out the cascade="all" for the "child" in the one-to-one, but I don't think that's a issue.

Try swapping your ID generators around, or changing your DB rule.

-G


Top
 Profile  
 
 Post subject: Parent gets inserted but not the child
PostPosted: Thu Oct 09, 2003 11:11 am 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
The parent (Vendor) gets inserted into the DB, but the Child (Contact) does not get inserted into the DB.

Are my mappings wrong? Please advise.

Below are my mappings:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.demo.hb.Vendor" table="HB_VENDOR">
               <id name="vendorId" column="VENDORID">
           <generator class="sequence">
              <param name="sequence">seq_vendorid</param>
           </generator>
          </id>
        <property name="vendorName" column="VENDORNAME"/>
        <one-to-one name="vendorContact" class="com.demo.hb.Contact"/>
    </class>
</hibernate-mapping>

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.demo.hb.Contact" table="HB_VENDOR_CONTACT">
       <id name="contactId" column="VENDORID">
            <generator class="foreign">
               <param name="property">contactVendor</param>
            </generator>
        </id>
        <property name="contactCity" column="CITY"/>
        <property name="contactCountry" column="COUNTRY"/>
        <property name="contactFirstName" column="CONTACTFIRSTNAME"/>
        <property name="contactLastName" column="CONTACTLASTNAME"/>
        <property name="contactLine1" column="ADDRESSLINE1"/>
        <property name="contactLine2" column="ADDRESSLINE2"/>
        <property name="contactState" column="STATE"/>
        <property name="contactTitle" column="CONTACTTITLE"/>
        <property name="contactZip" column="ZIP"/>
      <one-to-one name="contactVendor" class="com.demo.hb.Vendor" cascade="all" constrained="true"/>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Parent gets inserted but not the child
PostPosted: Thu Oct 09, 2003 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
sid wrote:
<one-to-one name="vendorContact" class="com.demo.hb.Contact"/>

Use cascade=none (default value) => parent updates, insert, delete won't be cascaded. Set this property explicitly.


Top
 Profile  
 
 Post subject: Only Parent (Vendor) gets inserted
PostPosted: Thu Oct 09, 2003 12:28 pm 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
I tried your suggestion. Only the Parent (Vendor) gets inserted. The Child (Contact) still does not get inserted.

Do I have to explicitly save the Child (Contact)? Or do I have to explicitly change the unsaved-value property for the id to be "0"?

Please advise. Below is my source code:
Code:
/ Instatiate Persistible Objects
         Vendor vendor = new Vendor();
         Contact contact = new Contact();
         
         //Copy ActionForm properties to Persistible properties
         BeanUtils.copyProperties(vendor,form);
         BeanUtils.copyProperties(contact,form);
         
         System.out.println("Contact First Name :" + (String)contact.getContactFirstName());
         // Set the Contact properties within Vendor
         vendor.setVendorContact(contact);
         //Instantiate the DAO.
          
           BaseDAO dao = new BaseDAO();
           dao.configure();
           boolean result = dao.addVendor(vendor);

Code:
public boolean addVendor(Vendor vendor) throws HibernateException {
      boolean result = false;

      Session session = sessionFactory.openSession();

      try {
         session.save(vendor);
         session.flush();
         result = true;
         session.connection().commit();
         session.close();
         
      } catch (HibernateException he) {
         result = false;
         throw he;
      } catch (SQLException sqe){
         result = false;
      }
      return result;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 12:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
I tried your suggestion

To set cascadeto all (not to none !)

Quote:
The unsaved-value attribute is important! If the identfier property of your class does not default to null, then you should specify the actual default.


Set cascade="all" and set unsaved-value to an appropriate value. And it will work as said in 7.7.1 section of Hibernate Reference Guide.


Top
 Profile  
 
 Post subject: Now Vendor is inserted but Contact is being updated?
PostPosted: Thu Oct 09, 2003 1:00 pm 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
So I corrected the mapping uising cascade ="all". Now the vendor gets inserted but the contact is being updated instead of being inserted!

I tried changing the unsaved-value from the deault "null" to "0" but then i get the error message "attempted to save a null"

Please help. What is unsaved-value used for? Why is the Child being updated when it actually needs to be inserted?

Below is the stack trace:
Code:
12:55:22,744  INFO Dialect:83 - Using dialect: net.sf.hibernate.dialect.Oracle9Dialect
12:55:22,754  INFO DriverManagerConnectionProvider:41 - Hibernate connection pool size: 20
12:55:22,754  INFO DriverManagerConnectionProvider:70 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@localhost:1521:JONE
12:55:22,754  INFO DriverManagerConnectionProvider:71 - connection properties: {user=ep10, password=ep10}
12:55:22,764  INFO SessionFactoryImpl:162 - Use outer join fetching: true
12:55:22,764 DEBUG DriverManagerConnectionProvider:77 - total checked-out connections: 0
12:55:22,764 DEBUG DriverManagerConnectionProvider:90 - opening new JDBC connection
12:55:23,615 DEBUG DriverManagerConnectionProvider:103 - created connection to: jdbc:oracle:thin:@localhost:1521:JONE, Isolation Level: 2
12:55:23,625 DEBUG DriverManagerConnectionProvider:117 - returning connection to pool, pool size: 1
12:55:23,625  INFO SessionFactoryImpl:185 - Use scrollable result sets: true
12:55:23,625  INFO SessionFactoryImpl:186 - JDBC 2 max batch size: 15
12:55:23,956 DEBUG SessionFactoryObjectFactory:39 - initializing class SessionFactoryObjectFactory
12:55:23,966 DEBUG SessionFactoryObjectFactory:76 - registered: 8a89e1d4f821c62300f821c628340000 (unnamed)
12:55:23,966  INFO SessionFactoryObjectFactory:82 - no JDNI name configured
12:55:23,976  INFO SessionFactoryImpl:269 - Query language substitutions: {}
12:55:23,976 DEBUG SessionFactoryImpl:281 - instantiated session factory
12:55:23,996 DEBUG SessionImpl:413 - opened session
12:55:24,006 DEBUG BatcherImpl:166 - about to open: 0 open PreparedStatements, 0 open ResultSets
12:55:24,006 DEBUG DriverManagerConnectionProvider:77 - total checked-out connections: 0
12:55:24,006 DEBUG DriverManagerConnectionProvider:83 - using pooled JDBC connection, pool size: 0
12:55:24,006 DEBUG SessionFactoryImpl:526 - prepared statement get: select seq_vendorid.nextval from dual
12:55:24,006 DEBUG SessionFactoryImpl:536 - preparing statement
12:55:24,046 DEBUG SequenceGenerator:70 - Sequence identifier generated: 100051
12:55:24,046 DEBUG BatcherImpl:173 - done closing: 0 open PreparedStatements, 0 open ResultSets
12:55:24,046 DEBUG SessionFactoryImpl:554 - closing statement
12:55:24,056 DEBUG SessionImpl:656 - saving [com.demo.hb.Vendor#100051]
12:55:24,066 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
12:55:24,076 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
12:55:24,076 DEBUG Cascades:208 - unsaved-value: 0
12:55:24,086 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
12:55:24,086 DEBUG Cascades:87 - cascading to saveOrUpdate()
12:55:24,096 DEBUG Cascades:208 - unsaved-value: 0
12:55:24,096 DEBUG SessionImpl:1201 - saveOrUpdate() unsaved instance with id: 0
12:55:24,116  WARN RequestProcessor:538 - Unhandled Exception thrown: class java.lang.NullPointerException
<Oct 9, 2003 12:55:24 PM EDT> <Error> <HTTP> <BEA-101017> <[ServletContext(id=32225808,name=hb,context-path=)] Root cause of ServletException.
java.lang.NullPointerException: attempted to save null
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:580)
        at net.sf.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:37)
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:599)
        at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1202)
        at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
        at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:739)
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
        at com.demo.hb.BaseDAO.addVendor(BaseDAO.java:46)
        at com.demo.hb.VendorRegistrationAction.execute(VendorRegistrationAction.java:53)
        at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
        at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
        at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
>


Top
 Profile  
 
 Post subject: bi-directional?
PostPosted: Thu Oct 09, 2003 7:38 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
It looks to me like you have a bidirectional relationship, if so, when you call
Code:
vendor.setVendorContact(contact);

does that code set the vendor on the Contact so that it has its relationship to its parent?

To summarize what others have already said but may not have been clear:
You need the cascade="all" on the parent, not the child where you have it at present.

I don't use one-to-one mappings so I'm not sure about the behaviour of the unsaved-value but when you associate the contact with the vendor that property would not be null so it might try to perform an update instead of a save... (? in which case you would need to save it explicitly I think) I do know that the unsaved-value is used to determine if the object needs to be inserted as a new row or if it just needs to be updated, so, if the unsaved-value does not match the value of your key it is assumed to need updating. You need to either explicitly save the object if there is no way of knowing it is new.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 2:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The unsaved-value is used by hibernate to "know" that an object must be saved (INSERT) or updated (UPDATE). It looks at the object id
if the id match the unsaved value, the object will be saved
if not it will be updated.

So the unsaved-value depends of your id type, here are some samples:
for a Long, the null value is a good unsaved value
for a long, 0 may be a good value, unless 0 is a PK in your DB
...

Here are the unsaved-value values :
- any - always save
- none - always update (default value for coimposite id - not your case))
- null - save when identifier is null (this is the default value for id)
- valid identifier value - save when identifier is null or the given value

I can't give you the appropriate solution, 'cause I don't have your object id type.

If you call save(parent) and have cascade="all", the saveOrUpdate(Child) will be called.

Hope this is clearer to you


[/list]


Top
 Profile  
 
 Post subject: Why does child have to set parent for the relationship?
PostPosted: Fri Oct 10, 2003 9:30 am 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
Thanks guys as per your suggestions I finally was able to get it to work. What I hadn't done was to setParent from the Child.

In my source I had just Vendor.setContact() (i.e. Parent.setChild()). I added Contact.setVendor() just before calling the above statement and it worked. So now my source code looks like this:

Code:
         Vendor vendor = new Vendor();
         Contact contact = new Contact();
         
         //Copy ActionForm properties to Persistible properties
         BeanUtils.copyProperties(vendor,form);
         BeanUtils.copyProperties(contact,form);
         
         // Set the Contact properties within Vendor
         contact.setVendor(vendor);
         vendor.setContact(contact);
         
                        //Instantiate the DAO. 
         BaseDAO dao = new BaseDAO();
         dao.configure();
          
         Long vendorId = (Long)dao.addVendor(vendor);


But my question is why do I need to setParent() from the child? This seems counter intuitive as all I should be doing is calling Parent.setChild().

Any thoughts on this...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 9:33 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you don't use Hibernate, only plain Java, is a bidirectional association created by only setting one side or do you have to set both?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 11:16 am 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
well, the main thrust of my question why does one-to-one mapping have to be bidirectional? I understand that if two objects are bidirectional then they have to set each others relationship.

So the first thing i tried was to have it unidirectional, but then HB would insert the child first and then the parent. So I reversed it, then it would create the parent but then try to update the child instead of inserting it. Then I tried the bidirectional with setting the parent from the child and ofcaourse it only inserted the parent.

So anyways to cut a loooong story short, i tried a number of permutations and combinations and only this bidirectional setup worked. What I am interested in knowing is:

1) Can you implement on-to-one mapping between a Parent and a Child (which has a foreign key constraint on the Parent's key)?

2) If it has to be bidirectional then does that automatically imply that the child has to set parent and the parent has to set its child?

Your thoughts please. To me it is intuitive to just do Parent.setChild() and then Parent.save().


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 11:20 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
<class name="Parent">
<many-to-one name="child" cascade="all" column="CHILD_ID"/>
</class>

Thats all.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 12:14 pm 
Newbie

Joined: Tue Oct 07, 2003 9:43 am
Posts: 16
Location: Chicago
Cool! I will try that out as well. So I guess I can use <many-to-one> even though my entities have a "one-to-one" relationship?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 12:53 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no difference if you look at the "one" side of this relationship, because it is unidirectional.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

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.