直接上源码大家看 帮帮忙
@Entity
@Component
public class Problems implements java.io.Serializable{
private int id;
private String title;
private String Content;//内容
private String Category;//类别
private String score;//积分
private String State;//状态
private String AddContent;//补充问题
// private Set<Answers> answers = new LinkedHashSet<Answers>();
private Set<Answers> answers ;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getAddContent() {
return AddContent;
}
public void setAddContent(String addContent) {
AddContent = addContent;
} @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
// @JoinTable(name = "rong_problems_answers", joinColumns = {@JoinColumn(name ="problems_id" )}, inverseJoinColumns = { @JoinColumn(name = "answer_id") })
// @OrderBy("id")
public Set<Answers> getAnswers() {
return answers;
}
public void setAnswers(Set<Answers> answers) {
this.answers = answers;
}}
@Entity
@Component
public class Answers implements java.io.Serializable {
private int id;
private String username;
private String Content;//内容
private Problems problems;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
@ManyToOne(cascade = { CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
// @JoinColumn(name="answer_id" )
public Problems getProblems() {
return problems;
}
public void setProblems(Problems problems) {
this.problems = problems;
}
然后生成的表 在answers表里有一个problemsID 我想每次在回答的时候将问题的id的保存进去 但是在beans里没有problemsid 我怎么才能将这个字段保存进去了数据库关系图 还有通过answers获取问题的HQL语句 谢谢大家
@Entity
@Component
public class Problems implements java.io.Serializable{
private int id;
private String title;
private String Content;//内容
private String Category;//类别
private String score;//积分
private String State;//状态
private String AddContent;//补充问题
// private Set<Answers> answers = new LinkedHashSet<Answers>();
private Set<Answers> answers ;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getAddContent() {
return AddContent;
}
public void setAddContent(String addContent) {
AddContent = addContent;
} @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
// @JoinTable(name = "rong_problems_answers", joinColumns = {@JoinColumn(name ="problems_id" )}, inverseJoinColumns = { @JoinColumn(name = "answer_id") })
// @OrderBy("id")
public Set<Answers> getAnswers() {
return answers;
}
public void setAnswers(Set<Answers> answers) {
this.answers = answers;
}}
@Entity
@Component
public class Answers implements java.io.Serializable {
private int id;
private String username;
private String Content;//内容
private Problems problems;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
@ManyToOne(cascade = { CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
// @JoinColumn(name="answer_id" )
public Problems getProblems() {
return problems;
}
public void setProblems(Problems problems) {
this.problems = problems;
}
然后生成的表 在answers表里有一个problemsID 我想每次在回答的时候将问题的id的保存进去 但是在beans里没有problemsid 我怎么才能将这个字段保存进去了数据库关系图 还有通过answers获取问题的HQL语句 谢谢大家
问题--->答案应该是1对多阿.!
import java.util.Date;
import java.util.HashSet;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;@Entity
public class ProductInfo implements Serializable{
private static final long serialVersionUID = -363596572933069914L;
private Long id;
private String shelflife; //产品保质期
private String code;//货号
private String name;//产品名称
private Brand brand;//品牌
private String model;//规格,规格
private Float baseprice;//底价(采购进来的价格)
private Float etprice;//市场价
private Float sellprice;//销售价
private Short weight;//重量
private Short netWeight;//净重
private String description;//产品简介
private String buyexplain;//购买说明
private Manufacturer manufacturer;//生产厂商
private Set<Provider> providers = new HashSet<Provider>();//供货商
private Set<ProductColor> productColors = new HashSet<ProductColor>();//产品的多种颜色,
private Set<ProductSize> productSizes = new HashSet<ProductSize>();//产品的多种尺码 ,现在用来表示口味
private Boolean visible = true; //是否可见
private ProductType producttype;//产品类型
private Date createdate = new Date();//上架日期
private Integer clickcount = 1;//人气指数
private Integer sellcount = 1;//销售量
private Boolean commend = false; //是否推荐
private SexRequest sexrequest = SexRequest.NONE;//性别要求
public ProductInfo() {}
public ProductInfo(Long id) {
this.id = id;
}
public Short getNetWeight() {
return netWeight;
} public void setNetWeight(Short netWeight) {
this.netWeight = netWeight;
} @Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable=false)
public Boolean getCommend() {
return commend;
}
public void setCommend(Boolean commend) {
this.commend = commend;
} @Enumerated(EnumType.STRING) @Column(nullable=false,length=6)
public SexRequest getSexrequest() {
return sexrequest;
}
public void setSexrequest(SexRequest sexrequest) {
this.sexrequest = sexrequest;
} @Column(nullable=false)
public Float getBaseprice() {
return baseprice;
}
public void setBaseprice(Float baseprice) {
this.baseprice = baseprice;
}
@ManyToOne(cascade={CascadeType.REFRESH},optional=true)
@JoinColumn(name = "brand_id", nullable=true)
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
@Column(length=80)
public String getBuyexplain() {
return buyexplain;
}
public void setBuyexplain(String buyexplain) {
this.buyexplain = buyexplain;
}
@Column(length=30)
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Lob
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} @Column(nullable=false)
public Float getMarketprice() {
return etprice;
}
public void setMarketprice(Float etprice) {
this.etprice = etprice;
}
@Column(length=30)
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Column(nullable=false, length=80)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(nullable=false)
public Float getSellprice() {
return sellprice;
}
public void setSellprice(Float sellprice) {
this.sellprice = sellprice;
}
@Column(nullable=false)
public Boolean getVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
public Short getWeight() {
return weight;
}
public void setWeight(Short weight) {
this.weight = weight;
}
@Column(nullable=false)
public Integer getClickcount() {
return clickcount;
} public void setClickcount(Integer clickcount) {
this.clickcount = clickcount;
}
@Column(nullable=false)
public Integer getSellcount() {
return sellcount;
} public void setSellcount(Integer sellcount) {
this.sellcount = sellcount;
} @Temporal(value=TemporalType.TIMESTAMP)
public Date getCreatedate() {
return createdate;
} public void setCreatedate(Date createdate) {
this.createdate = createdate;
} @ManyToOne(cascade=CascadeType.REFRESH,optional=true)
@JoinColumn(name = "manufacturer_id")
public Manufacturer getManufacturer() {
return manufacturer;
}
public void setManufacturer(Manufacturer manufacturer) {
this.manufacturer = manufacturer;
}
@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
@JoinTable(name="provider_product",joinColumns=@JoinColumn(name="product_id"),
inverseJoinColumns=@JoinColumn(name="provider_id"))
public Set<Provider> getProviders() {
return providers;
}
public void setProviders(Set<Provider> providers) {
this.providers = providers;
}
public void addProvider(Provider provider){
if(!this.providers.contains(provider))
this.providers.add(provider);
}
@OneToMany(mappedBy="product",cascade = {CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.EAGER)
@OrderBy(value = "visible desc, id asc")
public Set<ProductColor> getProductColors() {
return productColors;
}
public void setProductColors(Set<ProductColor> productColors) {
this.productColors = productColors;
}
public void addProductColor(ProductColor productColor) {
if (!this.productColors.contains(productColor)) {
productColor.setProduct(this);
this.productColors.add(productColor);
}
} public void removeProductColor(ProductColor productColor) {
if (this.productColors.contains(productColor)){
this.productColors.remove(productColor);
}
}
@OneToMany(mappedBy="product",cascade = {CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.EAGER)
@OrderBy(value = "visible desc, id asc")
public Set<ProductSize> getProductSizes() {
return productSizes;
}
public void setProductSizes(Set<ProductSize> productSizes) {
this.productSizes = productSizes;
}
public void addProductSize(ProductSize productSize) {
if (!this.productSizes.contains(productSize)) {
productSize.setProduct(this);
this.productSizes.add(productSize);
}
} public void removeProductSize(ProductSize productSize) {
if (this.productSizes.contains(productSize)){
this.productSizes.remove(productSize);
}
}
@ManyToOne(cascade={CascadeType.REFRESH},optional=true)
@JoinColumn(name = "producttype_id", nullable=false)
public ProductType getProducttype() {
return producttype;
}
public void setProducttype(ProductType producttype) {
this.producttype = producttype;
}
@Column(length=30)
public String getShelflife() {
return shelflife;
} public void setShelflife(String shelflife) {
this.shelflife = shelflife;
} /**
* 返回对象的散列代码值。该实现根据此对象
* 中 id 字段计算散列代码值。
* @return 此对象的散列代码值。
*/
@Override
public int hashCode() {
int hash = 0;
hash += (this.id != null ? this.id.hashCode() : 0);
return hash;
} /**
* 确定其他对象是否等于此 ProductInfo。当且仅当
* 参数不为 null 且该参数是具有与此对象相同 id 字段值的 ProductInfo 对象时,
* 结果才为 <code>true</code>。
* @param 对象,要比较的引用对象
* 如果此对象与参数相同,则 @return <code>true</code>;
* 否则为 <code>false</code>。
*/
@Override
public boolean equals(Object object) {
if (!(object instanceof ProductInfo)) {
return false;
}
ProductInfo other = (ProductInfo)object;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
return true;
} /**
* 返回对象的字符串表示法。该实现根据 id 字段
* 构造此表示法。
* @return 对象的字符串表示法。
*/
@Override
public String toString() {
return this.getClass().getName()+ "[id=" + id + "]";
}
}
这是我的一个实体类。你可以看看。
子类: 1. @ManyToOne (optional=true, fetch = FetchType.LAZY)
2. @JoinColumn(name="P_ID", nullable=true, insertable = false, updatable = false)
3. protected SAttribute sAttb; 父类: 1. @OneToMany(cascade = CascadeType.REFRESH, mappedBy = "sAttb", fetch = FetchType.LAZY)
2. protected Set<Child> child; 注意其中mappedBy是对应的.