-->
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.  [ 2 posts ] 
Author Message
 Post subject: one-to-one not correctly inserted.
PostPosted: Mon Apr 19, 2010 6:52 pm 
Newbie

Joined: Mon Apr 19, 2010 6:37 pm
Posts: 2
Hi guys,

I would appreciate any tips regarding this issue. I just can't get it working. I have two classes, Insurance and UploadedFile. Insurance has exactly one column referring to UploadedFile (primary key fileId).

The hibernate mappings:
Code:
<hibernate-mapping package="uws">
    <class name="Insurance">
        <id name="insuranceId">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="description"/>
        <one-to-one name="fileId" class="UploadedFile" cascade="all" constrained="true" lazy="false" fetch="join"/>
    </class>

    <class name="UploadedFile">
        <id name="fileId">
            <generator class="native"/>
        </id>
        <property name="fileName"/>
        <property name="path"/>
    </class>
</hibernate-mapping>


So when I try to insert it, hibernate inserts one row to insurance table and one row to UploadedFile, but obviously, I additionally want to map insurance.fileid to uploadedfile.fileid, otherwise I can't fetch a file that is related to insurance. The reason for separating uploadedfile is that it is used by other tables.

The insert is done as following:
Code:
final Insurance insurance=new Insurance();
insurance.setName(name);
final UploadedFile file=new UploadedFile();
file.setFileName(filename);
file.setPath(path);
insurance.setFileId(file);
HibernateCallback callback = new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException, SQLException {
       session.save(insurance);
       return null;
   }
};
hibernateTemplate.execute(callback);
   


Hibernate sqls:
Code:
Hibernate: insert into UploadedFile (fileId, fileName, path) values (null, ?, ?)
Hibernate: call identity()
Hibernate: insert into Insurance (insuranceId, name) values (null, ?)
Hibernate: call identity()

I am using hibernate version 3.5.0-Final.
Also tried saveOrUpdate() but no success. Any tips would be helpfull. Thanks.


Top
 Profile  
 
 Post subject: Re: one-to-one not correctly inserted.
PostPosted: Tue Apr 20, 2010 2:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
<one-to-one> maps the association using the primary keys and you don't need an extra column. One of the entities also needs to use the foreign id generator.

The other option is to use a <many-to-one> with a unique constraint. I think this is what you want. See http://docs.jboss.org/hibernate/stable/ ... n-onetoone for more information and some examples.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.