-->
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.  [ 14 posts ] 
Author Message
 Post subject: Problem with one-to-one mapping
PostPosted: Tue May 30, 2006 9:23 am 
Newbie

Joined: Tue May 30, 2006 8:25 am
Posts: 7
Hello...
i'm trying to use a simple one to one mapping but i had an exception when
i try to save a new instance of my object "Figlio", my settings are:

Hibernate version:
3.1.3

Mapping documents:
<?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="it.atscom.nes.model.Figlio"
table="FIGLIO"
>

<id
name="idFiglio"
column="ID_FIGLIO"
>

<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="foreign">

<!-- The generator-param tag is deprecated, use the @hibernate.generator and @hibernate.param tags instead -->
<param
name="property"
>
padre
</param>

</generator>

</id>

<one-to-one
name="padre"
constrained="true"
>

</one-to-one>

<property
name="descrizione1"
column="DES_FIGLIO_1"
length="50"
not-null="true"
>

</property>

<property
name="descrizione2"
column="DES_FIGLIO_2"
length="50"
not-null="true"
>

</property>

</class>

</hibernate-mapping>


<?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="it.atscom.nes.model.Padre"
table="PADRE"
>

<id
name="idPadre"
column="ID_PADRE"
type="java.lang.Long"
>

<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="native">

</generator>

</id>

<one-to-one
name="figlio"
>

</one-to-one>

<property
name="descrizione"
column="DES_PADRE"
length="50"
not-null="true"
>

</property>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Figlio figlio = new Figlio();
figlio.setDescrizione1("aaaa");
figlio.setDescrizione1("bbbb");

Padre padre = (Padre) dao.getObject(Padre.class, new Long(rb
.getString("padre.get.idPadre2")));

figlio.setPadre(padre);

dao.saveObject(figlio);

public class BaseDAOHibernate extends HibernateDaoSupport implements DAO {
protected final Log log = LogFactory.getLog(getClass());

/**
* @see it.atscom.nes.dao.DAO#saveObject(java.lang.Object)
*/
public void saveObject(Object o) {
getHibernateTemplate().saveOrUpdate(o);
}


Full stack trace of any exception that occurs:
[junit] Testcase: testInsertFiglioFromPadre(it.atscom.nes.dao.OneToOneDAOTest): Caused an ER
ROR
[junit] Unable to resolve property:
[junit] padre
[junit] ; nested exception is org.hibernate.HibernateException: Unable to resolve proper
ty:
[junit] padre
[junit]
[junit] org.springframework.orm.hibernate3.HibernateSystemException: Unable to resolve property:

[junit] padre
[junit] ; nested exception is org.hibernate.HibernateException: Unable to resolve proper
ty:
[junit] padre
[junit]
[junit] org.hibernate.HibernateException: Unable to resolve property:
[junit] padre
[junit]
[junit] at org.hibernate.tuple.EntityMetamodel.getPropertyIndex(EntityMetamodel.java:352)
[junit] at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplize
r.java:279)
[junit] at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractE
ntityPersister.java:3252)
[junit] at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:39)
[junit] at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSav
eEventListener.java:91)
[junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrReque
stedId(DefaultSaveOrUpdateEventListener.java:186)
[junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(Defaul
tSaveOrUpdateEventListener.java:175)
[junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(Defa
ultSaveOrUpdateEventListener.java:98)
[junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSa
veOrUpdateEventListener.java:70)
[junit] at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
[junit] at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
[junit] at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:490)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTe
mplate.java:690)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.ja
va:365)
[junit] at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTempla
te.java:687)
[junit] at it.atscom.nes.dao.hibernate.BaseDAOHibernate.saveObject(BaseDAOHibernate.java:28)

[junit] at it.atscom.nes.dao.OneToOneDAOTest.testInsertFiglioFromPadre(OneToOneDAOTest.java:
66)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java
:25)
[junit] Test it.atscom.nes.dao.OneToOneDAOTest FAILED

Name and version of the database you are using:
Oracle 9i

The generated SQL (show_sql=true):
[junit] ------------- Standard Output ---------------
[junit] Hibernate: select padre0_.ID_PADRE as ID1_88_1_, padre0_.DES_PADRE as DES2_88_1_, figlio
1_.ID_FIGLIO as ID1_89_0_, figlio1_.DES_FIGLIO_1 as DES2_89_0_, figlio1_.DES_FIGLIO_2 as DES3_89_0_
from PADRE padre0_ left outer join FIGLIO figlio1_ on padre0_.ID_PADRE=figlio1_.ID_FIGLIO where padr
e0_.ID_PADRE=?
[junit] ------------- ---------------- ---------------

Any suggestions?


Top
 Profile  
 
 Post subject: requires class parameter?
PostPosted: Tue May 30, 2006 10:11 am 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
How is your one-to-one mapped in the object? I may be mistaken but do you not need to supply a class value in the xml like this:

<one-to-one name="padre" class="it.atscom.nes.model.Padre" constrained="true" />


Top
 Profile  
 
 Post subject: Re: requires class parameter?
PostPosted: Tue May 30, 2006 10:19 am 
Newbie

Joined: Tue May 30, 2006 8:25 am
Posts: 7
rkhanmoh wrote:
How is your one-to-one mapped in the object? I may be mistaken but do you not need to supply a class value in the xml like this:

<one-to-one name="padre" class="it.atscom.nes.model.Padre" constrained="true" />


xml mapping is generated by xdoclet, by the way my 2 entity class are:
* @hibernate.class table="PADRE"
*
*/
public class Padre extends BaseObject implements Serializable {
private static final long serialVersionUID = 1L;
protected Long idPadre;
protected String descrizione;
protected Figlio figlio;

/**
* @hibernate.id generator-class="native" type="java.lang.Long"
* column="ID_PADRE"
*
*/
public Long getIdPadre() {
return this.idPadre;
}

public void setIdPadre(Long idPadre) {
this.idPadre = idPadre;
}

/**
* @return Returns the figlio.
* @hibernate.one-to-one name="figlio"
*/
public Figlio getFiglio() {
return figlio;
}

/**
* @param figlio The figlio to set.
*/
public void setFiglio(Figlio figlio) {
this.figlio = figlio;
}

/**
* @hibernate.property column="DES_PADRE" length="50" not-null="true"
* @struts.validator type="required" msgkey="errors.required"
*
*/
public String getDescrizione() {
return this.descrizione;
}

public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}

/**
* @see java.lang.Object#equals(Object)
*/
public boolean equals(Object object) {
return EqualsBuilder.reflectionEquals(this, object);
}

/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, false);
}

/**
* @see java.lang.Object#toString()
*/
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}

}

and

* @hibernate.class table="FIGLIO"
*
*/
public class Figlio extends BaseObject implements Serializable {
protected Long idFiglio;
protected Padre padre;
protected String descrizione1;
protected String descrizione2;

/**
* @hibernate.id generator-class="foreign" column="ID_FIGLIO"
* @hibernate.generator-param name="property" value="padre"
*/
public Long getIdFiglio() {
return this.idFiglio;
}

public void setIdFiglio(Long idFiglio) {
this.idFiglio = idFiglio;
}

/**
* @return Returns the padre.
* @hibernate.one-to-one constrained="true"
*/
public Padre getPadre() {
return padre;
}

/**
* @param padre The padre to set.
*/
public void setPadre(Padre padre) {
this.padre = padre;
}

/**
* @hibernate.property column="DES_FIGLIO_1" length="50" not-null="true"
*
*/
public String getDescrizione1() {
return this.descrizione1;
}

public void setDescrizione1(String descrizione1) {
this.descrizione1 = descrizione1;
}

/**
* @return Returns the descrizione2.
* @hibernate.property column="DES_FIGLIO_2" length="50" not-null="true"
*/
public String getDescrizione2() {
return descrizione2;
}

/**
* @param descrizione2
* The descrizione2 to set.
*/
public void setDescrizione2(String descrizione2) {
this.descrizione2 = descrizione2;
}

/**
* @see java.lang.Object#equals(Object)
*/
public boolean equals(Object object) {
return EqualsBuilder.reflectionEquals(this, object);
}

/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, false);
}

/**
* @see java.lang.Object#toString()
*/
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 11:22 am 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
ah ok, no real experience with xdoclet, just looks like it would need to know the type it's mapping to within the hibernate mapping.

Can you manually edit them? Just wondering if perhaps it's not being generated correctly. Similar to property's which need 'type', I am guessing it would need a 'class' value.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 11:33 am 
Newbie

Joined: Tue May 30, 2006 8:25 am
Posts: 7
rkhanmoh wrote:
ah ok, no real experience with xdoclet, just looks like it would need to know the type it's mapping to within the hibernate mapping.

Can you manually edit them? Just wondering if perhaps it's not being generated correctly. Similar to property's which need 'type', I am guessing it would need a 'class' value.


i did it but it doesn't work..i dont know why


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 12:06 pm 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
try adding the line:

@hibernate.one-to-one class="it.atscom.nes.model.Padre"


to

/**
* @return Returns the padre.
* @hibernate.one-to-one constrained="true"
*/

might need to hide or remove the contrained value temporarily until it works, not sure if you can use two at the same time


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 2:56 am 
Newbie

Joined: Tue May 30, 2006 8:25 am
Posts: 7
the attribute class for xdoclet isn't mandatory, by the way your suggest doesn't work :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 10:13 am 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
my bad, appears you don't need it. i assume that without these one-to-one's it works fine? Just wondering if the padre object loads into hibernate without the one-to-one mapping (i assume it does, just confirming)

From the error i don't think the issue is with your one to one mapping so much as hibernates attempt to map the hbm.xml file to the .java file for your figlio object at all. otherwise i'm sorry, that's my experience with one-to-one mappings expunged, but i'll keep looking if i get the time


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 11:51 am 
Newbie

Joined: Mon Sep 13, 2004 8:20 am
Posts: 7
Location: Poland
From what i see you want to have one-to-one association on primary-key whichg means both primary keys in both tables are the same

For this you need in your Padre xml file special id generator

<id name="id" column="ID_FIGLIO">
<generator class="foreign">
<param name="property">figlio</param>
</generator>
</id>
<one-to-one name="figlio" constrained="true"/>

if not then consider association on foreign key with many-to-on unique="true"


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 12:07 pm 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
good spot - just noticed the <param name="property">padre</param>
entry - do you need this? if you have the one-to-one anyway, is this necessary over a unique id per object?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 4:50 am 
Newbie

Joined: Tue May 30, 2006 8:25 am
Posts: 7
what i need is this:
TABLE PADRE
------------------
ID_PADRE (PK)
DES_PADRE

TABLE FIGLIO
------------------
ID_PADRE(FK)
DES_FIGLIO_1
DES_FIGLIO_2


Top
 Profile  
 
 Post subject: Problem with one-to-one mapping
PostPosted: Thu Jun 01, 2006 5:41 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi Weirdo,

I have similar problem and I think I know why

one-to-one mapping like

<class name="it.atscom.nes.model.Figlio" table="FIGLIO">

<id name="idFiglio" column="ID_FIGLIO">
<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="foreign">
<!-- The generator-param tag is deprecated, use the @hibernate.generator and @hibernate.param tags instead -->
<param name="property">padre</param>
</generator>
</id>

<one-to-one name="padre" constrained="true"></one-to-one>
...

</class>

with the other one

<class name="it.atscom.nes.model.Padre" table="PADRE">

<id name="idPadre" column="ID_PADRE" type="java.lang.Long">

<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="native"></generator>

</id>

<one-to-one name="figlio">
</one-to-one>

cause following when try to save new instance but no problem manipulating
existing instances

[junit] Unable to resolve property:
[junit] padre
[junit] ; nested exception is org.hibernate.HibernateException: Unable to resolve property:
[junit] padre
........

now notice 'constrained="true"' in the mapping above, this enforces 'foreign key constraint'
(or referential constraint), which means for every new Figlio instance you are trying to
create, an existing instance of the parent class 'Padre' must also exist to match the
foreign key of Figlio instance.

Now according to 'Hibernate in Action', this mapping will create the same pk on both
tables hence if you try to add a new Figlio instance with null id or an id which
doesn't have corresponding match in Padre ===> exception.

But according to the unit test I runned, it looks all you need to save
a new instance of child class (i.e Figlio) is to set its id and it doesn't
matter if the id didn't have a matchind id value from parent (i.e. Padre).



Regards,


Sam


Top
 Profile  
 
 Post subject: Re: Problem with one-to-one mapping
PostPosted: Thu Jun 01, 2006 6:37 am 
Newbie

Joined: Tue May 30, 2006 8:25 am
Posts: 7
ok Sam i've solved the problems too... the problem is generated by xdoclet.
hibernate-param outputs whitespace that Hibernate doesn't like.
http://www.mail-archive.com/xdoclet-dev ... 18832.html


Top
 Profile  
 
 Post subject: one-to-one problem
PostPosted: Fri Jun 02, 2006 12:36 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Does this mean you can add new instance of Figlio and let DB auto-increment your id for Figlio???

i.e.
Figlio fig = new Figlio();
dao.saveFig(fig)

I would be surprised if this works without
assigning an id explicitlyto Figlio 1st.

i.e.
Figlio fig = new Figlio();
fig.setId(new Long(7));
dao.saveFig(fig)


Thanks,

Sam


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.