3

请问有人可以帮助我吗?当我在 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;
}

}    

分类帐类

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]

请有人帮助我..:(

4

1 回答 1

9

您忘记添加 ac.addAnnotatedClass(Ledger.class); 到您的 Hibernate 配置

以及QuantityBought_

无论如何,您不想SessionFactory为每个 servlet 调用创建一个

于 2013-03-28T14:34:44.993 回答