三个@Entity类Tc, Tracer, Track是三向关联的的表, @Embeddable类TcTracer则存储着三个表的ForeignKey
@Entity
@Table(name= "tc", catalog="first_hiber")
public class Tc {
@org.hibernate.annotations.CollectionOfElements
@JoinTable(name="tc_tracer", joinColumns=@JoinColumn(name="tc_id"))
private Set<TcTracer> tcTracers = new HashSet<TcTracer>();
@Id @GeneratedValue
@Column(name="tc_id", nullable= false)
private Long id;
}
@Embeddable
@Table(name="tc_tracer")
public class TcTracer {
@org.hibernate.annotations.Parent
private Tc tc;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="tracer_id", nullable=false, updatable=false)
private Tracer tracer;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="track_id", nullable=false, updatable=false)
private Track track;
}
如上,将TcTracer作为Tc的子元素,从而实现Tc的三相关联。
用HQL可以通过特定的Tc,Tracer得到对应的所有Track:
select tctracers.track
from Tc tc
join tc.tcTracers tctracers
where tc.name="tc name"
and tctracers.tracer.name="tracer name"
List<Track> tracks = session.createQuery(HQL).list();
posted on 2007-04-02 17:45
hijeff 阅读(374)
评论(0) 编辑 收藏 所属分类:
Hibernate