-->
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.  [ 2 posts ] 
Author Message
 Post subject: Entity with multiple variables of the same embedable type
PostPosted: Wed Jul 15, 2009 7:49 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
Ok so here's my situation. I have two classes which use JPA annotations to describe schema.

Class A is a simple class that looks something like this...
Code:
@Embeddable
public class A
{
    private String name;
    private Object value;
}


and I have class B which contains two members of type A like so...
Code:
@Entity
@Table(name = "table_b")
public class B
{
    private A member1;
    private A member2;
}


Now when I compile and run my code I noticed two things. First, I don't see any fields in the generated database schema for either of member1 or member2. If I have only one member of type A I normally get two columns in my database; one for name and one for value. Instead i'm just getting runtime errors that hibernate can't figure out what to do. What is wrong with my code? How can I get two members of the same type to be embeddable or is this simply a bug of hibernate?


Top
 Profile  
 
 Post subject: Re: Entity with multiple variables of the same embedable type
PostPosted: Wed Jul 15, 2009 8:49 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
After a bit more research I was able to find a solution. It turns out you can override the column definitions. Doing so seems to make hibernate and the database schema happy.
I found the information at http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#Embedded.

In case anyone is curious here's what my class B now looks like as a result.

Code:
@Entity
@Table(name = "table_b")
public class B
{
    @Embedded
    @AttributeOverrides({
       @AttributeOverride(name = "name", column=@Column(name = "member1_name")),
       @AttributeOverride(name = "value", column=@Column(name = "member1_value"))
    })
    private A member1;

    @Embedded
    @AttributeOverrides({
       @AttributeOverride(name = "name", column=@Column(name = "member2_name")),
       @AttributeOverride(name = "value", column=@Column(name = "member2_value"))
    })
    private A member2;
}


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