-->
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: Hibernate composite keys with auto-increment columns problem
PostPosted: Thu Sep 02, 2010 5:05 pm 
Newbie

Joined: Thu Sep 02, 2010 5:01 pm
Posts: 2
Hi,

I've found a big issue with Hibernate 3 (using annotations). I'll try to explain it with a little example.

I have a table with a composite key of four columns. One of such columns is an auto-increment column:

CREATE TABLE IF NOT EXISTS `bdprueba`.`extensions` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`context` VARCHAR(40) NOT NULL DEFAULT '' ,
`exten` VARCHAR(40) NOT NULL DEFAULT '' ,
`priority` INT(11) NOT NULL DEFAULT '0' ,
`app` VARCHAR(40) NOT NULL DEFAULT '' ,
`appdata` VARCHAR(256) NOT NULL DEFAULT '' ,
PRIMARY KEY (`id`, `context`, `exten`, `priority`) )
ENGINE = MyISAM
AUTO_INCREMENT = 11
DEFAULT CHARACTER SET = latin1;


With the following class mapping (+ composite pk class):


package ext;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

import com.sun.istack.internal.NotNull;

@Entity
@Table(name = "extensions")
@IdClass(ExtensionPK.class)
public class Extension implements Serializable {



public Extension(String context, String extension, int priority) {
super();
this.context = context;
this.extension = extension;
this.priority = priority;
}



@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;

@Id
@NotNull
@Column(name = "context", length = 40)
private String context;

@Id
@NotNull
@Column(name = "exten", length = 40)
private String extension;

@Id
@NotNull
@Column(name = "priority")
private int priority;

@NotNull
@Column(name = "app", length = 40)
private String application;

@NotNull
@Column(name = "appdata", length = 256)
private String applicationData;

[gets and sets]

}



@Embeddable
public class ExtensionPK implements Serializable {

public ExtensionPK() {
super();
}


@NotNull()
@Column(name = "id")
private int id;

@NotNull()
@Column(name = "context", length = 40)
private String context;

@NotNull
@Column(name = "exten", length = 40)
private String extension;

@NotNull
@Column(name = "priority")
private int priority;

[.. gets and sets]

}

With this mapping, the persistence of an 'Extension' object works well. However, I need to know the key assigned to the 'id' attribute after an extension is stored (o.getId()), but surprisingly it's always 0!. This works well when I have a single primary key (I mean, after storing an object with an auto-generated key, I can know the assigned id just by checking the attribute).

Anybody know what may be the problem?, My real scenario is with a more complex dbmodel. I need to know the assigned value of an element of a composite-pk (after saving its related object) to be able to create other related composite-pk-objects. I simply don't want to query the object again (by this way, the pk-values are stored on the object correctly).

Thanks in advance!

Héctor


Top
 Profile  
 
 Post subject: Re: Hibernate composite keys with auto-increment columns problem
PostPosted: Fri Sep 03, 2010 1:50 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't think Hibernate has support for generated composite keys. The documentation at http://docs.jboss.org/hibernate/stable/ ... ompositeid says:

Quote:
You cannot use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.


Top
 Profile  
 
 Post subject: Re: Hibernate composite keys with auto-increment columns problem
PostPosted: Fri Sep 03, 2010 5:36 pm 
Newbie

Joined: Thu Sep 02, 2010 5:01 pm
Posts: 2
Hi Nordborg, thanks for your reply, and specially thanks for your documentation's reference.

Let me propose additional questions...

- about this limitation, can't it be considered as a Hibernate's big issue?, because this framework is supposed to do everything that can be done with the old JDBC and SQL!... and I don't like the idea of alter my DB model or forget RDBMS features just because ORM limitations.
- What could be the best approach to handle this scenario?... I mean, ¿what could be the best strategy for the application to assign identifiers by itself?, are there any application-based key-generation strategy safe for concurrency?
- Somebody suggest me to use 'GenerationType.IDENTITY' as generation strategy, but with no more details. Is it right?


Thanks,
Héctor


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.