-->
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: [Annotations] Using multiple @Embedded objects of same type
PostPosted: Thu Mar 13, 2008 6:42 am 
Newbie

Joined: Thu Mar 13, 2008 5:59 am
Posts: 3
Hi,

I am trying to map an entity with two embedded properties of the same type, using annotations. This works as long as the properties of the embedded entity are not a reference to another entity.

My example:

Class Amount
Code:
@Embeddable
public class Amount
{
    private double amount;
    private String currencyCode;
...
}

Class Account
Code:
@Entity
public class Account
{
    @Id
    private Long id;
    private String accountName;
    @Embedded
    private Amount creditLimit;
    @Embedded
    private Amount availableCredit;
...
}

This does not work, as Hibernate tries to map both creditLimit.amount and availableCredit.amount to the same column amount in the generated SQL table.

Ok, we need to change the annotations:
Class Account, 2nd version
Code:
@Entity
public class Account
{
  @Id
  private Long id;
  private String accountName;
  @Embedded
  @AttributeOverrides( {
    @AttributeOverride(name="amount", column = @Column(name="creditLimit_amount") ),
    @AttributeOverride(name="currencyCode", column = @Column(name="creditLimit_currencyCode") )
    } )
    private Amount creditLimit;

  @Embedded
  @AttributeOverrides( {
    @AttributeOverride(name="amount", column = @Column(name="availableCredit_amount") ),
    @AttributeOverride(name="currencyCode", column = @Column(name="availableCredit_currencyCode") )
    } )
    private Amount availableCredit;
...
}


This works nicely and generates a table with the following columns:
Code:
id
accountName
creditLimit_amount
creditLimit_currencyCode
availableCredit_amount
availableCredit_currencyCode


My first question here: is there a way to make Hibernate automatically prefix the the column name with the property name of the embedded object (so we do not need to write this ugly @AttributeOverrides construct)?

But of course our Amount class does not store a currencyCode, but a reference to a Currency object:

Class Currency
Code:
@Entity
public class Currency
{
    @Id
    private Long id;
    private String code;
    private String name;
    private int precision;
...
}

Class Amount, 2nd version
Code:
@Embeddable
public class Amount
{
    private double amount;
    @ManyToOne
    private Currency currency;
...
}


Now I don't find a way of telling Hibernate to use creditLimit_currency_id for the reference to property creditLimit.currency. Both

Code:
...
  @Embedded
  @AttributeOverrides( {
    @AttributeOverride(name="amount", column = @Column(name="creditLimit_amount") ),
    @AttributeOverride(name="currency", column = @Column(name="creditLimit_currency") )
    } )
...


and

Code:
...
  @Embedded
  @AttributeOverrides( {
    @AttributeOverride(name="amount", column = @Column(name="creditLimit_amount") ),
    @AttributeOverride(name="currency_id", column = @Column(name="creditLimit_currency_id") )
    } )
...


are simply ignored. When Hibernate tries to build my database schema, the first Amount creates a table column currency_id, and the second Amount (avaliableCredit) tries to create the same column, resulting in the exception:
Code:
org.hibernate.MappingException: Repeated column in mapping for entity: Account column: currency_id (should be mapped with insert="false" update="false")


Any hints on this?

Best Regards,

Christoph Jäger


Top
 Profile  
 
 Post subject: Found solution: @AssociationOverride
PostPosted: Thu Mar 13, 2008 7:13 am 
Newbie

Joined: Thu Mar 13, 2008 5:59 am
Posts: 3
Of course, after writing all of this, and searching a bit more, I found the solution: @AssociationOverride is the annotation to use.

In class Account (see parent post), the annotations need to look like:

Code:
...
  @Embedded
  @AttributeOverride(name="amount", column=@Column(name="creditLimit_amount"))
  @AssociationOverride(name="currency", joinColumns=@JoinColumn(name="creditLimit_currency_id"))
    private Amount creditLimit;

  @Embedded
  @AttributeOverride(name="amount", column=@Column(name="availableCredit_amount"))
  @AssociationOverride(name="currency", joinColumns=@JoinColumn(name="availableCredit_currency_id"))
    private Amount availableCredit;
...


Now no exceptions are thrown and the database schema generated looks as expected.


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.