-->
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: composite-id
PostPosted: Thu Sep 25, 2008 6:07 am 
Newbie

Joined: Wed Sep 24, 2008 11:34 am
Posts: 3
hello everyone,
my name is Albert. I spent the last two weeks trying to solve a problem about composite-id. I'm getting crazy.

I have three tables whom mapping is the following:

##############Table PI_LAVORATORE###########

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="PiLavoratore" table="PI_LAVORATORE" >
<id name="keyPiLavoratore" type="java.lang.Long">
<column name="KEY_PI_LAVORATORE" precision="9" scale="0" />
<generator class="sequence">
<param name="sequence" >SEQ_PI_LAVORATORE</param>
</generator>
</id>

<many-to-one name="piDeduz" class="Deduzione" fetch="join" cascade="save-update">
<column name="FK_PI_DEDUZ_KEY_PI_LAVORATORE" precision="9" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="tipocontratto" class="TipoContratto" fetch="join" cascade="refresh">
<column name="FKTIPOCONTR_KEY_PI_LAVORATORE" precision="9" scale="0" />
</many-to-one>


.....other properties


<set name="pilavOptionses" inverse="true" fetch="join" cascade="all, delete-orphan" >
<key>
<column name="FK_LAV_KEY_PILAV_OPTIONS" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="PilavOptions" />
</set>
</class>
</hibernate-mapping>


##############Table PILAV_OPTIONS###########
please note that this xml file is completely pasted, as you can figure out, there are no properties other than two fields that represent the foreign keys of two different tables.
So this is the only purpose of this table.


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="PilavOptions" table="PILAV_OPTIONS" >
<composite-id name="id" class="PilavOptionsId">
<key-many-to-one name="piLavoratore" class="PiLavoratore" >
<column name="FK_LAV_KEY_PILAV_OPTIONS" precision="9" scale="0" />
</key-many-to-one>
<key-many-to-one name="options" class="Options">
<column name="FK_OPTIONS_KEY_PILAV_OPTIONS" precision="9" scale="0" />
</key-many-to-one>
</composite-id>
<many-to-one name="piLavoratore" class="PiLavoratore" update="false" insert="false" fetch="join">
<column name="FK_LAV_KEY_PILAV_OPTIONS" precision="9" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="options" class="Options" update="false" insert="false" fetch="join">
<column name="FK_OPTIONS_KEY_PILAV_OPTIONS" precision="9" scale="0" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>




##############Table OPTIONS###########à


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Options" table="OPTIONS" >
<id name="keyOptions" type="java.lang.Long">
<column name="KEY_OPTIONS" precision="9" scale="0" />
<generator class="native" />
</id>
<property name="nameOptions" type="java.lang.String">
<column name="NAME_OPTIONS" length="8" not-null="true" />
</property>

................other properties


<set name="pilavOptionses" inverse="true" fetch="join" cascade="all, delete-orphan">
<key>
<column name="FK_OPTIONS_KEY_PILAV_OPTIONS" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="PilavOptions" />
</set>
</class>
</hibernate-mapping>

##################################

The most important classes that it's worth showing here are obviously:

public class PilavOptionsId implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private PiLavoratore piLavoratore;
private Options options;

public PilavOptionsId() {
}

##################################

public class PilavOptions implements java.io.Serializable {

private static final long serialVersionUID = 1L;


private PilavOptionsId id;
private PiLavoratore piLavoratore;
private Options options;

##################################

public class Options implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private static final Log log = LogFactory.getLog(Options.class);



private Long keyOptions;
private String nameOptions;
private String descOptions;
private Long rankOptions;
private Set<PilavOptions> pilavOptionses = new HashSet<PilavOptions>(0);



##################################

public class PiLavoratore implements java.io.Serializable {

private static final long serialVersionUID = 1L;


private Long keyPiLavoratore;
private Deduzione piDeduz;
private TipoContratto tipocontratto;

..........other properties

private Set <PilavOptions> pilavOptionses = new HashSet<PilavOptions>(0);

###################################

And here's the problem.
When I try to create a new instance of

Session hs = HibernateSessionFactory.getSession();
Transaction tx = hs.beginTransaction();

PiLavoratore lavoratore= new PiLavoratore();
PilavOptions pilav = new PilavOptions();
Options opt=(Options)hs.get(Options.class, new Long(oneKey));
PilavOptionsId pilavId = new PilavOptionsId(lavoratore,opt);
pilav.setId(pilavId);
pilav.setOptions(opt);
pilav.setPiLavoratore(lavoratore);
hs.saveOrUpdate(pilav);

tx.commit();
hs.refresh(DeduzioneObject);

so when I refresh the Deduzione object, that is PiLavoratore's father object, everything's ok. I look at the log and it shows, correctly, an insert of the new lavoratore, and insert of the new pilavoptions and an update of the Deduzione object to notify the PiLavoratore's insert.

So far so good.

The disaster comes over when I try to have an update:

suppose I want to modify the option property of the
pilavoption object:


//suppose PiLavoratore lavoratore = (..)hs.get(...) from db
//get the Pilavoption from the PiLavoratore object I'm working with
pilavOption = new PilavOptions();
Iterator<PilavOptions>it=lavoratore.getPilavOptionses().iterator();
pilavOption = it.next();

//load a new option object
Options opt=(Options)hs.get(Options.class, new Long(oneKey));
PilavOptionsId pilavId = new PilavOptionsId(lavoratore,opt);
pilavOption .setId(pilavId);
pilavOption .setOptions(opt); //this is the new option
pilavOption .setPiLavoratore(lavoratore);
hs.saveOrUpdate(pilav);

doesn't work!!!!
I mean the log tace everything but instead of updating the pilavOption db record it inserts a new one.....
###############
It doesn't work even if I only set the option property without changing the Id property
#####
I'm supposed to have tried almost everything to achieve this goal.

I took a look to the forum topics and to many mailing lists but I still haven't found anyone who uses a table with only two properties that are even two foreign keys and have mapped them with <key-many-to-one.

So please help.
Hope I've been clear enough during my explanation.
Thank you so much in advance.

ps. I want to apologise for my english.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 10:09 am 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
Hi there,

If you are only looking to associate a PiLavoratore object with an Option object you don't need a class to sit on top of your joining table. You can give your PiLavoratore a list of Options using the join table and a one-to-many relationship.

Some good info and an example here:
http://www.hibernate.org/hib_docs/refer ... -join.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 11:37 am 
Newbie

Joined: Wed Sep 24, 2008 11:34 am
Posts: 3
Hello there,

first of all thank you so much for replying.
Unfortunatelly I cannot modify anything.
They gave me the db and the 'xml'.

So the only thing I can do is try to make it works.
Otherwise I will have to make a delete-element from the set and than to insert another record with the modifications needed....
I know it's a dirty solutions but what else can I do.

Hope to solve it.
Once again...thank you for helping me out.


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.