2013-03-28 24 views
3

請認真地幫助我?我得到當我保存數據Client.class運行時異常:使用@OneToMany或@ManyToMany以未映射的類爲目標

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] 

請有人幫助我.. :(

回答

9

你忘了添加 ac.addAnnotatedClass(Ledger.class); 到Hibernate配置

QuantityBought以及

反正你不希望創建一個SessionFactory每個servlet的調用

+0

thanx很多orid..i jst gt2知道它..但nw m開了另一個錯誤.. o rg.hibernate.HibernateException:不能同時獲取多個行包 –

+0

您必須顯示完整跟蹤。它的基本意思是在一個映射實體中有兩個用@OneToMany(... fetch = FetchType.EAGER ...)註解的同一類中的列表。這不支持Hibernate –

+0

thaanxx很多人.. :) –

相關問題