-->
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.  [ 8 posts ] 
Author Message
 Post subject: shared primary keys
PostPosted: Tue May 16, 2006 1:04 pm 
Newbie

Joined: Tue May 16, 2006 12:49 pm
Posts: 8
Hi!

I've some trouble with a OneToOne mapping. It's just like:

Code:
public class VoucherOrder {

    @Id
    @Column(name = "ID_VoucherOrder")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_Id")
    public Long getId() {
        return id;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public OrderSupplement getDetails() {
        return details;
    }

}

public class OrderSupplement {

    @Id
    @Column(name = "ID_VoucherOrder")
    public Long getId() {
        return id;
    }

}


Pretty much the same like the Heart/Body example from the hibernate documentation. Now if I try to persist a VoucherOrder with an OrderSupplement set, I get this ids need to set manually error.

In Google's cache (hibernate manually primarykeyjoincolumn), I found an appropriate remark for version 3.1 beta 4, which is no longer stated. Seems like Hibernate doesn't realize that the ID should be taken from the voucherorder.

It seems to me that this is a rather common situation, so does anybody have an idea how I can let Hibernate handle this? Sure, I may save the order first and afterwards set the supplement, but I don't like that...

Sebastian


Hibernate version:
Hibernate Annotations Version: 3.1beta9

Code between sessionFactory.openSession() and session.close():
OrderSupplement oS = new OrderSupplement();
oS.setText("Blabla");
VoucherOrder v = new VoucherOrder();
v.setAmount(new BigDecimal("12.34"));
v.setCurrency("EUR");
v.setDetails(oS);
session.save(v);

Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): net.paybox.voucher.oi.OrderSupplement
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:506)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:498)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:437)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:326)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:534)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:522)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at net.paybox.voucher.oi.TestVouchers.testCreate(TestVouchers.java:38)
at net.paybox.voucher.oi.TestVouchers.main(TestVouchers.java:18)
Name and version of the database you are using:
HSQLDB 1.8.0
The generated SQL (show_sql=true):
%
Debug level Hibernate log excerpt:
%
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 6:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
in order supplement use a @GenericGenerator(strategy="foreign", parameters...)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: IdentifierGenerationException
PostPosted: Sat Jun 24, 2006 1:04 am 
Newbie

Joined: Sat Jun 24, 2006 12:35 am
Posts: 5
Hi,
I would like to specify my own custom class for Identifier generation. I am using the @GenericGenerator and have specified the following in my code:

@Id
@GenericGenerator(name="blah",strategy="com.company.myFullyQualifedClass")
@Column(name="player_Id") private String id;

public String getId() {
return this.id;
}

This is the stackTrace obtained:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Players
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId
(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGene
ratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrR
equestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTran
sient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdae(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:559)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:543)
at Client.main(Client.java:37)


As a matter of fact I get this same error even when I specify any built in strategy like increment. Can someone point to me what I am doing wrong. I am using hibernate-annotations-3.1beta8.

Thanks,
Priya


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 24, 2006 3:54 am 
Newbie

Joined: Tue May 16, 2006 12:49 pm
Posts: 8
Hi Priya!

Sorry I cannot help you. I still have the same problem, this custom strategy didn't work out for me either.
In my situation, I handle the insert by manually doing this:
* store the Order
* copy the order's ID to the supplement
* store the supplement
This is not what I expected from Hibernate, but it works...

Cheers,
Sebastian


Top
 Profile  
 
 Post subject: IdentifierGenerationException
PostPosted: Sat Jun 24, 2006 5:39 pm 
Newbie

Joined: Sat Jun 24, 2006 12:35 am
Posts: 5
Hi Sebastian,

Thanks for your response. I would like to use the custom generation feature as this is one of great features provided by the hibernate core. Even if the annotations dont work I would like to know how I can contribute to fix this. Can someone point to me where to start? I looked into the GenericGenerator interface... and I am not sure which all classes implement this for the built in strategy like increment,native, etc. Should I write my own class which implements the GenericGenerator interface? The API doc for GenericGenerator says "No usage of org.hibernate.annotations.GenericGenerator" and I am not sure what I need to do. If I can fix this I will gladly contribute to the community...
Any information is appreciated.

Thanks,
Priya


Top
 Profile  
 
 Post subject: IdentifierGenerationException
PostPosted: Mon Jun 26, 2006 3:05 pm 
Newbie

Joined: Sat Jun 24, 2006 12:35 am
Posts: 5
I tried the following and it works

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy="customGUIDGenerator")

@Column(name="player_Id")
private String id;
public String getId() {
return this.id;
}


With this I can specify my own GUIDGenerator.

Thanks,
Priya


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 2:47 pm 
Newbie

Joined: Fri Jul 14, 2006 10:52 am
Posts: 16
emmanuel wrote:
in order supplement use a @GenericGenerator(strategy="foreign", parameters...)


So is the documentation with the body -- heart example incorrect then since is suggests one can have a unidirectional one-to-one relation with a shared common key, or is this discussion way over my head (not an uncommon phenomenon)????


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 4:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no the example in the doc is fine, but you have do set the id value yourself

_________________
Emmanuel


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