-->
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.  [ 7 posts ] 
Author Message
 Post subject: Annotation help for list of children with composite key
PostPosted: Fri Feb 10, 2006 9:00 am 
Newbie

Joined: Thu Jun 30, 2005 6:58 am
Posts: 5
Hibernate version:3.1.1
Hibernate annotations version:3.1beta8

Hi,

I'm having real trouble converting a mapping file to annotations for what is conceptually a fairly simple case. What I have is a PropertyDefinition class that can have 0 or more PropertyParams. The legacy database I am working with defines a composite primary key on the child table, consisting of the propertyId of the parent, and an index.

In my mapping document, I specify this as follows:

Mapping documents:
Code:
   <class name="PropertyDefinition" table="PROPERTY_DEFINITION" >
      <id name="propertyId" column="I_PROPERTY_ID" >
         <generator class="native" >
            <param name="sequence">PROPERTY_SEQ</param>
         </generator>
      </id>
                ...
      <list name="propertyParams" lazy="false" cascade="all" >
         <key>
            <column name="I_PROPERTY_ID" />
         </key>
         <list-index column="I_PARAM_ID" base="1" />
         <one-to-many class="PropertyParams" />
      </list>       
   </class>
   
   <class name="PropertyParams" table="PROPERTY_PARAMS" >
      <composite-id>
         <key-many-to-one name="propertyDefinition" column="I_PROPERTY_ID" class="PropertyDefinition" />
         <key-property name="paramId" column="I_PARAM_ID" />
      </composite-id>
      <property name="label" column="C_LABEL" />
      <property name="value" column="C_VALUE" />
   </class>


This works fine, but I'd really like to be able to use annotations in place of mapping files, since there are a large number of classes to map.

What I am struggling with is how to convert this mapping file into the equivalent annotations. My class definitions look something like the following (edited for brevity):

Code:
@Entity
@Table(name = "PROPERTY_DEFINITION")
@SequenceGenerator(name = "PropertyIdGenerator", sequenceName = "PROPERTY_SEQ")
public class PropertyDefinition
    implements java.io.Serializable
{

    // Fields
    private long propertyId;
    private List<PropertyParams> propertyParams = new ArrayList<PropertyParams>( 0 );

    // Property accessors
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator="PropertyIdGenerator")
    @Column(name = "I_PROPERTY_ID", unique = true, nullable = false, insertable = true, updatable = true, precision = 22, scale = 0)
    public long getPropertyId() {
        return this.propertyId;
    }

    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "propertyDefinition")
    @IndexColumn(name="I_PARAM_ID", base=1)
    public List<PropertyParams> getPropertyParams() {
        return this.propertyParams;
    }
}


@Entity
@Table(name = "PROPERTY_PARAMS", uniqueConstraints = { @UniqueConstraint(columnNames = {
        "I_PROPERTY_ID", "C_LABEL" }) })
@IdClass(PropertyParamsId.class)
public class PropertyParams
    implements java.io.Serializable
{

    // Fields
//    private PropertyParamsId id;
    //private long propertyId;
    private PropertyDefinition propertyDefinition;
    private long paramId;

    @Id
    @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumn(name = "I_PROPERTY_ID", unique = false, nullable = false, insertable = false, updatable = false)
    public PropertyDefinition getPropertyDefinition() {
        return this.propertyDefinition;
    }

    @Id
    public long getParamId() {
        return paramId;
    }
}


@Embeddable
public class PropertyParamsId
    implements java.io.Serializable
{

    // Fields
    //private long propertyId;
    private PropertyDefinition propertyDefinition;
    private long paramId;

    public PropertyDefinition getPropertyDefinition() {
        return this.propertyDefinition;
    }


    public void setPropertyDefinition(PropertyDefinition propertyDefinition) {
        this.propertyDefinition = propertyDefinition;
    }
   
   
    public long getParamId() {
        return this.paramId;
    }
}



The mixture of composite keys, embeddable's, and list index columns is proving too much to grasp... I've read the documentation and looked for examples, but just haven't been able to work out the right combination of field types and annotations.

Any assistance would therefore be very much appreciated!

Thanks,[/code]

_________________
Adam


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 10:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
<key-many-to-one> is not yet supported

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Annotation help for list of children with composite key
PostPosted: Sun Feb 12, 2006 3:08 pm 
Newbie

Joined: Thu Jun 30, 2005 6:58 am
Posts: 5
OK, thanks for clearing that up. Guess I'll have to stick with XML mappings for now.

Cheers,

_________________
Adam


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 2:20 pm 
Newbie

Joined: Tue Sep 02, 2003 3:14 pm
Posts: 11
Any idea when it might be supported ? If it's a bad design, what is the clean way to do this given you have a class which associates 2 entities and few attributes ?

Thanks in advance for your advise.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 25, 2006 9:04 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
it will be in the next release to be due soon.
The clean design is to use a surrogate key

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 1:33 pm 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
So, what is the equivalent of a key-many-to-one in the annotation world?

It does not seem there is an equivalent?

I thing this would help me share pk value of parent in child composite keys. If not what else is available for that?


Top
 Profile  
 
 Post subject: Foreign keys in composite primary keys
PostPosted: Fri Feb 06, 2009 12:41 pm 
Newbie

Joined: Fri Feb 06, 2009 12:27 pm
Posts: 1
Location: France, Sophia-Antipolis
Hi,

I'm sorry to come back to that old thread but I hope that Emmanuel is alerted and can reply.

Emmanuel, you said in 2003 that it will be in next release, but I don't think it is and what I see is that the <key-many-to-one> is still not well supported. Please corect me if I'm wrong.

About that issue, what I understand (from Hibernate in Action) is that we need to map one property for the primary key, and one many-to-one relationship with insertable=false and updateable=false.
But that has two drawbacks:
- either both properties must be set (duplication of code) or we need to flush and refresh the object (roundtrip to the database)
- the UML model is not very nice as the same attribute is seen both as an attribute and an association

Please, can you tell us if that issue is planned to be solved or not.


About the 'clean design is to use a surrogate key' response, I would like to explain a bit why it is not a good solution in some situations.
The situation where foreign key is part of the primary key is something that is often encountered in relational databases. It is what makes them both logically well designed and physically very efficient.

This is known as 'identifying relationship': an association can be part of the key as well as an attribute.


I can give a lot of examples. But basically, adding surrogate keys in those cases will:

- need an additional index (the primary key index + a unique index on the natural key)
That as a bad effect on performance (double the database work for inserts) and storage

- often need an additional join. When using composite natural keys for referential integrity, information is propagated to detail tables, without any denormalization. Using surrogate key, you often need to join to the master table to get the same information. That has a bad effect on performance.

- lower the possibilities to partition big tables (for oracle for example, you can't partition a unique index if the partition key is not part of the table primary key)

- at least for oracle, the optimizer will do better choices when using composite natural keys as it has statistics about all columns.

Of course, the workaround introducing a surrogate key for Hibernate is acceptable in small applications, but I'm currently dealing with a big application that manages large volume. I appreciate a lot the usage of Hibernate annotations to handle hundreds of business objects, but I can't ignore the performance constraints when building the data model.

I'll will appreciate a lot if we can have a vision of future improvements about that in hibernate.

Thanks in advance,

Franck.


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