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
|