简体   繁体   中英

How to use Hibernate annotations

I have following tables in in DB: Tables

and want to have hibernate @Entity ExchangeRates like followng POJO:

class ExchangeRates{
    List<Currency> currencies;
}

class Currency{
    Long Id;
    String name;
    List<Rate> exchangeRates;
}

class Rate{
    Currency currency;
    BibDecimal rate;
}

can any advice me how to do this with hibernate annotations?

If you have two Entities with a ManyToMany relationship you will have a JoinTable that references both.

The Table would eg look like this:

Name: EX_CUR
Column 1:CUR_ID
Column 2:EX_ID

And the Entities like this:

@Entity
@Table(name="currencies_fixed_exchange_rates")
class ExchangeRates{

    @Id
    Long id;

    @ManyToMany
    @JoinTable(
      name="EX_CUR",
      joinColumns=@JoinColumn(name="CUR_ID", 
      referencedColumnName="ID"),
      inverseJoinColumns=@JoinColumn(name="EX_ID", 
      referencedColumnName="ID"))
    List<Currency> currencies;
}

@Entity
@Table(name="currencies")
class Currency{

    @Id
    Long Id;

    @Column
    String name;

    @ManyToMany(mappedBy="currencies")
    List<Rate> exchangeRates;
}

@Entity
@Table
class Rate{

    @Id
    Long id;

    @OneToOne
    Currency currency;

    @Column
    BigDecimal rate;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM