-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping a collection of entities from two different classes
PostPosted: Thu Sep 11, 2008 4:39 am 
Newbie

Joined: Tue Jan 29, 2008 6:37 pm
Posts: 9
Hi,

I am trying to map 2 different parent classes with a one-to-many to a set of child classes of the same type, but then hibernate complains that _propertyNameBackref is already mapped.

This is the current situation:

Code:
// The parent classes
@Entity
@Table(name="t_holiday")
public class Holiday implements Serializable {
   private static final long serialVersionUID = -3948723008730842068L;
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   
   @OneToMany(cascade=CascadeType.ALL)
   @Cascade(value={org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   @JoinColumn(name="item_id", nullable=false)
   private Set<Translation> names;
        ...
}

@Entity
@Table(name="t_request_type")
public class RequestType implements Serializable {
   private static final long serialVersionUID = -5099401234648927543L;
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   
   @OneToMany(cascade=CascadeType.ALL)
   @Cascade(value={org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   @JoinColumn(name="item_id", nullable=false)
   private Set<Translation> names;
   
   @OneToMany(cascade=CascadeType.ALL)
   @Cascade(value={org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   @JoinColumn(name="item_id", nullable=false)
   private Set<Translation> descriptions;
        // getters and setters omitted
}

// the child class
@Entity
@Table(name="t_translations")
public class Translation implements Serializable {
   private static final long serialVersionUID = -6518609736903575223L;

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   
   @Column(name="locale", nullable=false)
   private String locale;
   private String description;
   
   @Enumerated(EnumType.STRING)
   private TranslationCategory category;
        // getters and setters ommitted
}


The exception I get is this:
Code:
org.hibernate.MappingException: Duplicate property mapping of _namesBackref found in be.xplore.verlof.model.Translation
   at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:477)
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:467)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1135)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:816)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:734)
   at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1333)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1299)
   ... 120 more



I tried this with both HIbernate 3.2.6 and 3.3.0 (GA) but they both give the same error, and I know there was a resolved JIRA issue for this:
http://opensource.atlassian.com/project ... e/HHH-2598

However, since I still have this issue in the latest version I assume I am doing this the wrong way.
I really wouldn't know how I could map this kind of relationship differently, any help is welcome here.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 12:37 pm 
Newbie

Joined: Thu Oct 26, 2006 11:50 am
Posts: 17
Location: Chesterfield, VA
You need to create a many to any relationship.

http://xdoclet.sourceforge.net/xdoclet/ ... -tags.html

I hope that helps some.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:31 pm 
Newbie

Joined: Tue Jan 29, 2008 6:37 pm
Posts: 9
Hmm, I have looked at the annotation example for a manytoany relationship, however I have some difficulties understanding some things.

example from docs:
Code:
@ManyToAny(
            metaColumn = @Column( name = "property_type" ) )
    @AnyMetaDef(
        idType = "integer",
        metaType = "string",
        metaValues = {
            @MetaValue( value = "S", targetEntity = StringProperty.class ),
            @MetaValue( value = "I", targetEntity = IntegerProperty.class ) } )
    @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
    @JoinTable( name = "obj_properties", joinColumns = @JoinColumn( name = "obj_id" ),
            inverseJoinColumns = @JoinColumn( name = "property_id" ) )
    public List<Property> getGeneralProperties() {


Does @MetaValue( value = "S", targetEntity = StringProperty.class mean that if property_type = "S" Hibernate should map it to a StringProperty.class ?
the @JoinColumn("obj_id") is the foreign key in the obj_properties table and tthe inverseJoinColumn "property_id" , is that the primary key of obj_properties ?

Please correct me if I'm wrong, since this was not clarified in the documentation at
http://www.hibernate.org/hib_docs/annot ... -extratype


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:56 pm 
Newbie

Joined: Thu Oct 26, 2006 11:50 am
Posts: 17
Location: Chesterfield, VA
Yes you need to have a column which identifies which class type Hibernate is to use. The key in that column can be anything you want, so long as it is defined the hbm file.

So this example choose to use S for String and I for Integer.

I have used Classnames before.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 4:05 pm 
Newbie

Joined: Tue Jan 29, 2008 6:37 pm
Posts: 9
ah :)


However I still do not see how this can help me though, My list of Translation objects must be Translation Objects and not other objects :s

edit: I did some trial and error and I got this to work fine :)


Indeed it implied also having some extra tables but at least everything works now :)

Thanks!!


Top
 Profile  
 
 Post subject: Solution?
PostPosted: Fri Jan 02, 2009 8:35 pm 
Newbie

Joined: Sun Feb 18, 2007 1:05 pm
Posts: 16
Hi I am facing the same issue ....could you please share what is the solution/work around you have implemented?

Thanks in advance
Shaiju


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