Hibernate配置对象关联关系

//实体类

public class Tbs implements java.io.Serializable {

// Fields

private Integer id;
private Zbs zbs;
private String tbqy;
private String content;
private Date dates;
private Double price;
private String state;

// Constructors

/** default constructor */
public Tbs() {
}

/** minimal constructor */
public Tbs(Integer id) {
this.id = id;
}

/** full constructor */
public Tbs(Integer id, Zbs zbs, String tbqy, String content, Date dates,
Double price, String state) {
this.id = id;
this.zbs = zbs;
this.tbqy = tbqy;
this.content = content;
this.dates = dates;
this.price = price;
this.state = state;
}

// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Zbs getZbs() {
return this.zbs;
}

public void setZbs(Zbs zbs) {
this.zbs = zbs;
}

public String getTbqy() {
return this.tbqy;
}

public void setTbqy(String tbqy) {
this.tbqy = tbqy;
}

public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}

public Date getDates() {
return this.dates;
}

public void setDates(Date dates) {
this.dates = dates;
}

public Double getPrice() {
return this.price;
}

public void setPrice(Double price) {
this.price = price;
}

public String getState() {
return this.state;
}

public void setState(String state) {
this.state = state;
}

}

 

public class Zbs implements java.io.Serializable {

// Fields

private Long id;
private String name;
private String contents;
private Date enddate;
private String state;
private Set tbses = new HashSet();

// Constructors

/** default constructor */
public Zbs() {
}

/** minimal constructor */
public Zbs(Long id, String name) {
this.id = id;
this.name = name;
}

/** full constructor */
public Zbs(Long id, String name, String contents, Date enddate,
String state, Set tbses) {
this.id = id;
this.name = name;
this.contents = contents;
this.enddate = enddate;
this.state = state;
this.tbses = tbses;
}

// Property accessors

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getContents() {
return this.contents;
}

public void setContents(String contents) {
this.contents = contents;
}

public Date getEnddate() {
return this.enddate;
}

public void setEnddate(Date enddate) {
this.enddate = enddate;
}

public String getState() {
return this.state;
}

public void setState(String state) {
this.state = state;
}

public Set getTbses() {
return this.tbses;
}

public void setTbses(Set tbses) {
this.tbses = tbses;
}

}

public class Demo {

/** 测试类
* @param args
*/
public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();
/* Tbs tbs=(Tbs) session.get(Tbs.class, 1);
System.out.println("项目名称"+"投标企业"+"报价");
System.out.println(tbs.getZbs().getName());
System.out.println(tbs.getZbs().getName()+tbs.getTbqy()+tbs.getPrice());*/
Zbs zbs=(Zbs) session.get(Zbs.class, (long)4);
Set<Tbs> tbs=zbs.getTbses();
String string="";
for (Tbs tbs2 : tbs) {

string+=tbs2.getTbqy()+",";

}
System.out.println("编号"+"项目名称"+"企业名称");
System.out.println(zbs.getId()+zbs.getName()+string);

}

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。