-->
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: one-to-many mapping help please
PostPosted: Mon Jun 28, 2004 1:09 pm 
Newbie

Joined: Sun Oct 26, 2003 4:46 pm
Posts: 16
Location: Tallinn, Estonia
Hello,

I have the following table structure:

Code:
ACCOUNTS
  ID : VARCHAR(32) PK
  TYPE : CHAR(1)

BALANCES
  ACCOUNT_ID : VARCHAR(32) PK
  CURRENCY : CHAR(3) PK
  BALANCE : NUMERIC(16,2)

In other words there are accounts which can have balances in several currencies. The BALANCES table's primary key consists of two fields.

I have an Account class:
Code:
/**
* @hibernate.class table="ACCOUNTS"
*/
public class Account {

  private String id;
  private char type;
  private Set balances;

  /**
   * @hibernate.id generator-class="assigned"
   */
  public String getId() { return id; }
  public void setId(String id) { this.id = id; }

  /**
   * @hibernate.property
   */
  public char getType() { return type; }
  public void setType(char type) { this.type = type; }

  /**
   * @hibernate.set
   * @hibernate.collection-key column="ACCOUNT_ID"
   * @hibernate.collection-???????
   */
  public Set getBalances() { return balances; }
  public void setBalances(Set balances) { this.balances = balances; }
}

And a Balance class:
Code:
public class Balance {

  private String currency;
  private BigDecimal amount;

  public String getCurrency() { return currency; }
  public void setCurrency(String currency) { this.currency = currency; }

  public BigDecimal getAmount() { return amount; }
  public void setAmount(BigDecimal amount) { this.amount = amount; }
}


And the question is what do I write in place of the question marks in the Account.getBalances() javadoc?

Initially I thought that one-to-many would be great but as the PK for BALANCES table is in two fields then I would probably have to add the accounId field to Balance class?

Another option I thought of was using the Balance as a component. This means I should implement equals() and hashCode() in Balance. In the equals method - would it be ok to compare the values of the currency fields?

Are there any other possibilities?

Any advice would be greatly appreciated,

_________________
best regards,
erik


Top
 Profile  
 
 Post subject: Re: one-to-many mapping help please
PostPosted: Mon Jun 28, 2004 3:13 pm 
Newbie

Joined: Wed May 12, 2004 5:12 pm
Posts: 8
Location: Frankfurt / Germany
I think you should add the account to the balance classe since a balance has an account, and than go ahead with
/**
* @hibernate.set
* @hibernate.collection-key column="ACCOUNT_ID"
* @hibernate.collection-one-to-many class="Balance"
*/
public Set getBalances() { return balances; }


This is just a guess but I think it should work out for you. Of course, you have to map Balance as well.


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.