Client.class
package com.pojo;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Client implements Serializable {
private String Name;
private String Address;
private float balance;
@Id
@GeneratedValue
private int client_id;
@OneToMany(targetEntity=Ledger.class,mappedBy="client",cascade= CascadeType.ALL,fetch= FetchType.EAGER)
private List<Ledger> ledger;
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
public List<Ledger> getLedger() {
return ledger;
}
public void setLedger(List<Ledger> ledger) {
this.ledger = ledger;
}
public String getAddress() {
return Address;
}
public void setAddress(String Address) {
this.Address = Address;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public int getClient_id() {
return client_id;
}
public void setClient_id(int client_id) {
this.client_id = client_id;
}
}
Ledger.class
package com.pojo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class Ledger implements Serializable {
@Id
private int id;
@ManyToOne
private Client client;
private Date date_of_purchase;
@OneToMany(targetEntity=QuantityBought.class,mappedBy="belongs_to_ledger",cascade= CascadeType.ALL,fetch= FetchType.EAGER)
private List<QuantityBought> quantitybought;
private float sum_total;
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public Date getDate_of_purchase() {
return date_of_purchase;
}
public void setDate_of_purchase(Date date_of_purchase) {
this.date_of_purchase = date_of_purchase;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<QuantityBought> getQuantitybought() {
return quantitybought;
}
public void setQuantitybought(List<QuantityBought> qb) {
this.quantitybought = qb;
}
public float getSum_total() {
return sum_total;
}
public void setSum_total(float sum_total) {
this.sum_total = sum_total;
}
}
当我通过servlet添加数据时:
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
AnnotationConfiguration ac=new AnnotationConfiguration();
ac.addAnnotatedClass(Client.class);
ac.configure();
SessionFactory factory=ac.buildSessionFactory();
Session s=factory.openSession();
try {
Client c=new Client();
c.setAddress(request.getParameter("address"));
c.setBalance(Float.parseFloat(request.getParameter("balance")));
c.setName(request.getParameter("name"));
QuantityBought qb=new QuantityBought();
Ledger l=new Ledger();
l.getQuantitybought().add(qb);
c.getLedger().add(l);
s.beginTransaction().begin();
s.save(c);
s.beginTransaction().commit();
response.sendRedirect("../Project_pepsi/front.jsp");
}catch(Exception e)
{
out.print(e);
}
finally {
out.close();
s.close();
factory.close();
}
显示此错误..
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.pojo.Client.ledger[com.pojo.Ledger]
请有人帮帮我.. :(
答案 0 :(得分:9)
你忘了添加
ac.addAnnotatedClass(Ledger.class);
到您的Hibernate配置
和QuantityBought
以及
无论如何,你不想为每个servlet调用创建一个SessionFactory