-->
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: OneToOne and OneToMany conflict
PostPosted: Mon Jul 03, 2006 4:35 am 
Newbie

Joined: Fri Nov 25, 2005 12:48 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.0-CR1/ Hibernate annotations version: 3.1.0-beta10

Name and version of the database you are using : mysql Ver 12.22 Distrib 4.0.24

Hi,


I have a question : I have to make the mapping of a class "CV"

which has the following properties :

@SuppressWarnings("serial")
@Entity
public class CV implements ICV
{
private Long id;

private IMissionGroup defaultGroup = new MissionGroup("Groupe par defaut");
private List<IMissionGroup> missionGroups = new ArrayList<IMissionGroup>();

/**
* @return Returns the defaultGroup.
*/
@OneToOne(mappedBy="cv", targetEntity=MissionGroup.class, cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public IMissionGroup getDefaultGroup()
{
return defaultGroup;
}
/**
* @param defaultGroup The defaultGroup to set.
*/
public void setDefaultGroup(IMissionGroup defaultGroup)
{
this.defaultGroup = defaultGroup;
}

/**
* @return Returns the missionGroups.
*/
@OneToMany(targetEntity=MissionGroup.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@JoinColumn(name="cv_id")
public List<IMissionGroup> getMissionGroups()
{
return missionGroups;
}
/**
* @param missionGroups The missionGroups to set.
*/
public void setMissionGroups(List<IMissionGroup> missionGroups)
{
this.missionGroups = missionGroups;
}



// other getters and setters

}

A mission belongs to a missionGroup

On the webapp, when I create a mission; I have to choose the missionGroup it will belongs to (list of selection :
"none" ->> defaultGroup
"mymissionGroup1"
"mymissionGroup2" ..etc
If I didn't create any missionGroups, the mission belongs to the defaultGroup by default.

The problem is that I have both a oneToOne and a oneToMany relationship between CV and MissionGroup, that's why, when I try to get the defaultGroup, I have a conflict :

Quote:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate3.HibernateSystemException: More than one row with the given identifier was found: 1, for class: fr.model.hibernate.MissionGroup; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: fr.model.hibernate.MissionGroup
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:406)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:348)



because in the meantime.. I have added some missionGroups ...

to avoid the problem, I actually put @Transient to the getDefaultGroup() method , that's mean each time I create a mission, I'd better create a missionGroup before, then I must select a missionGroup for the mission created.
And that's mean the defaultGroup is now totally useless ...

can the problem be solved in another way?


Last edited by Danzzz on Mon Jul 03, 2006 5:42 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 5:41 am 
Newbie

Joined: Fri Nov 25, 2005 12:48 pm
Posts: 6
i've added some @Where clause)
Code:

    /**
     * @return Returns the defaultGroup.
     */
    @OneToOne(mappedBy="cv", targetEntity=MissionGroup.class, cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @Where(clause="label like 'Groupe par defaut'")
    public IMissionGroup getDefaultGroup()
    {
        return defaultGroup;
    }

    /**
     * @return Returns the missionGroups.
     */
    @OneToMany(targetEntity=MissionGroup.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name="cv_id")
   @Where(clause="label not like 'Groupe par defaut'")
    public List<IMissionGroup> getMissionGroups()
    {
        return missionGroups;
    }



still get the same error when i load the cv with the ID = 1....
Quote:

org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: fr.model.hibernate.MissionGroup



By the way, I can see :

Quote:
11:54:17,627 INFO [STDOUT] Hibernate: select missiongro0_.id as id5_0_, missiongro0_.label as label5_0_, missiongro0_.dateStart as dateStart5_0_, missiongro0_.dateEnd as dateEnd5_0_, missiongro0_.cv_id as cv5_5_0_ from MissionGroup missiongro0_ where missiongro0_.cv_id=?


11:54:17,781 INFO [STDOUT] Hibernate: select missiongro0_.cv_id as cv5_1_, missiongro0_.id as id1_, missiongro0_.id as id5_0_, missiongro0_.label as label5_0_, missiongro0_.dateStart as dateStart5_0_, missiongro0_.dateEnd as dateEnd5_0_, missiongro0_.cv_id as cv5_5_0_ from MissionGroup missiongro0_ where missiongro0_.label not like 'Groupe par defaut' and missiongro0_.cv_id=?




I don't have the "where clause" : "where label like 'Groupe par defaut'. For the oneToOne relationSHip, can't we use the "Where clause" ??!

any idea?


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.