Thursday, May 14, 2009

Sample of Hibernate Annotation on Entity Bean Associations/Relationships

1. one-one
@Entity
public class Body {
@Id
public Long getId() { return id; }

@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Heart getHeart() {
return heart;
}
...
}

@Entity
public class Heart {
@Id
public Long getId() { ...}
}


@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="passport_fk")
public Passport getPassport() {
...
}

@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = "passport")
public Customer getOwner() {
...
}


2. many-one
    @ManyToOne(fetch = FetchType.EAGER)
@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name = "SecurityID")
public SecurityMaster getSecurity()
{
return this.security;
}



3. one-many
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "transactionId")
@OrderBy("transactionTaxlotId asc")
public Set<TransactionTaxlot> getTransactionTaxlots() {
return transactionTaxlots;
}



4. many-many
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "trelTrader",
joinColumns = {
@JoinColumn(name="traderId", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="fundId")
}
)
public Set<Fund> getFunds() {
return this.funds;
}

2 comments:

  1. waht is a manytomany I have in the interim table more than 2 columns, as can be mapped?


    mariano.dagostino@gmail.com

    ReplyDelete
  2. I'm sorry but could you give me more detail about it?

    ReplyDelete