2015-05-06 76 views
0

任何人都可以幫助我解決這個問題嗎?我得到這個錯誤如下:線程「主」異常org.hibernate.AnnotationException mappedBy參考

Exception in thread "main" org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: model.entities.Project.Responsible in model.entities.User.projects 
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:708) 
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668) 
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69) 
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1636) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1390) 
    at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:941) 
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:188) 
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:156) 
    at model.dao.ProjectTest.schema(ProjectTest.java:21) 
    at model.dao.ProjectTest.main(ProjectTest.java:56) 

我試着在論壇上發佈的所有解決方案徒勞。我的實體是:

package model.entities; 

import java.io.Serializable; 
import java.util.Set; 

import javax.persistence.*; 



    @Entity 
    @Table(name="users") 
    public class User implements Serializable{ 




     /** 
     * 
     */ 
     private static final long serialVersionUID = 3044688521472776233L; 
     int id_user; 
     public String firstname; 
     public String lastname; 
     public String login; 
     public String password; 
     public String service_line; 

     /**************/ 
     @Transient 
     public Set<Project> projects; 

     /**************/ 
     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     @Column(name = "id_user") 
     public int getId_user() { 
      return id_user; 
     } 

     @Column(name = "firstname") 
     public String getFirstname() { 
      return firstname; 
     } 
     public void setFirstname(String firstname) { 
      this.firstname = firstname; 
     } 


     @Column(name = "lastname") 
     public String getLastname() { 
      return lastname; 
     } 
     public void setLastname(String lastname) { 
      this.lastname = lastname; 
     } 

     @Column(name = "login") 
     public String getLogin() { 
      return login; 
     } 
     public void setLogin(String login) { 
      this.login = login; 
     } 

     @Column(name = "password") 
     public String getPassword() { 
      return password; 
     } 
     public void setPassword(String password) { 
      this.password = password; 
     } 

     @Column(name = "service_line") 
     public String getService_line() { 
      return service_line; 
     } 
     public void setService_line(String service_line) { 
      this.service_line = service_line; 
     } 



     /*****************/ 
     @OneToMany(mappedBy="responsible") 
     public Set<Project> getProjects() 
     { 
      return projects; 
     } 
     public void setProjects(Set<Project> projects) 
     { 
      this.projects = projects; 
     } 

    } 

    package model.entities; 

    import java.io.Serializable; 
    import java.sql.Date; 
    import java.util.Set; 

    import javax.persistence.*; 


    @Entity 
    @Table(name = "project") 
    public class Project implements Serializable{ 


    /** 
    * 
    */ 
    private static final long serialVersionUID = 974608598281873370L; 

    int id_project; 

    public String name; 

    public String type; 

    public String project_EM; 

    public String phase; 

    public int sub_project; 

    public String service_model; 

    public String project_status; 

    public String standby_status_explanation; 

    public String nb_fte; 

    public boolean monitoring_exemption; 

    public int declines; 

    public Date decline_date; 

    /************/ 
    @Transient 
    private User responsible; 

    /*****************/ 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id_project") 
    public int getId_project() { 
    return id_project; 
} 

@Column(name = "project_name") 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
@Column(name = "project_type") 
public String getType() { 
    return type; 
} 
public void setType(String type) { 
    this.type = type; 
} 
@Column(name = "project_EM") 
public String getProject_EM() { 
    return project_EM; 
} 
public void setProject_EM(String project_EM) { 
    this.project_EM = project_EM; 
} 
@Column(name = "phase") 
public String getPhase() { 
    return phase; 
} 
public void setPhase(String phase) { 
    this.phase = phase; 
} 
@Column(name = "nbr_sub_project") 
public int getSub_project() { 
    return sub_project; 
} 
public void setSub_project(int sub_project) { 
    this.sub_project = sub_project; 
} 
@Column(name = "service_model") 
public String getService_model() { 
    return service_model; 
} 
public void setService_model(String service_model) { 
    this.service_model = service_model; 
} 
@Column(name = "status") 
public String getProject_status() { 
    return project_status; 
} 
public void setProject_status(String project_status) { 
    this.project_status = project_status; 
} 
@Column(name = "standby_status_explanation") 
public String getStandby_status_explanation() { 
    return standby_status_explanation; 
} 
public void setStandby_status_explanation(String standby_status_explanation) { 
    this.standby_status_explanation = standby_status_explanation; 
} 

@Column(name = "nbr_fte") 
public String getNb_fte() { 
    return nb_fte; 
} 
public void setNb_fte(String nb_fte) { 
    this.nb_fte = nb_fte; 
} 

@Column(name = "monitoring_exemption") 
public boolean isMonitoring_exemption() { 
    return monitoring_exemption; 
} 
public void setMonitoring_exemption(boolean monitoring_exemption) { 
    this.monitoring_exemption = monitoring_exemption; 
} 

@Column(name = "declines") 
public int getDeclines() { 
    return declines; 
} 
public void setDeclines(int declines) { 
    this.declines = declines; 
} 
@Column(name = "decline_date") 
public Date getDecline_date() { 
    return decline_date; 
} 
public void setDecline_date(Date decline_date) { 
    this.decline_date = decline_date; 
} 

public Project() 
{ 
    super(); 
} 

public Project(String name,String type, String project_em,String phase,String status) 
{ 
    this.name=name; 
    this.type=type; 

} 
/*************/ 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name="id_user",referencedColumnName="id_user", nullable = false) 
    public User getUser() 
    { 
     return responsible; 
    } 
    public void setUser(User responsible) 
    { 
     this.responsible =responsible; 
    } 

} 

package model.dao; 

import model.entities.Project; 
import model.entities.User; 

import org.hibernate.*; 
import org.hibernate.cfg.*; 
import org.hibernate.tool.hbm2ddl.SchemaExport; 

import util.HibernateUtil; 

public class ProjectTest { 

public static void schema() 
{ 
    Configuration config= new Configuration(); 
    config.addAnnotatedClass(User.class); 
    config.addAnnotatedClass(Project.class); 

    config.configure("hibernate.cfg.xml"); 
    new SchemaExport(config).create(true, true);; 
} 

public static void addProject(String name, String type, String EM, String phase, String model, String status) { 

    Session session = HibernateUtil.getSessionFactory().openSession(); 
    Transaction tx = session.beginTransaction(); 

    User user=new User(); 
    user.setFirstname("Maryam"); 
    user.setLastname("maryam"); 
    user.setLogin("ma"); 
    user.setPassword("ma"); 
    session.save(user); 
    Project ar = new Project(); 

    ar.setName(name); 
    ar.setType(type); 
    ar.setProject_EM(EM); 
    ar.setPhase(phase); 
    ar.setService_model(model); 
    ar.setProject_status(status); 
    ar.setUser(user); 
    user.getProjects().add(ar); 




    session.save(ar); 
    tx.commit(); 
    session.close(); 
} 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    schema(); 
    addProject("a","b","nicolas","c","BASIC","execution"); 
} 

} 

項目實體中存在映射引用「負責任」,但仍不知道錯誤在哪裏。 在此先感謝

回答

0

您配置mappedBy綁定到負責任的財產,但使用getter + setter與不同的名稱,這可能會造成您的問題。根據標準 - > getResponsible/setResponsible重命名getter setter。

除此之外,是否有一個特定的原因,這個@Transient在memeber(這可能是hibernate沒有找到它的原因)和@JoinColumn在getter上。我沒有看到這一點。我只想把ManyToOne放在現場。

+0

謝謝你的回覆。我實際上解決了這個問題。我將註釋移至屬性而不是方法。 – maryam

相關問題