有人可以帮我解决这个问题吗?我在下面收到此错误:
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");
}
}
映射的参考&#34;负责&#34;存在于项目实体中,但仍然不知道错误的位置。 提前致谢
答案 0 :(得分:0)
您将mappedBy绑定配置为负责的属性,但使用带有不同名称的getter + setter可能会产生您的问题。根据标准重命名吸气剂设定器 - &gt; getResponsible / setResponsible。
除此之外,这个@Transient在memeber上有一个特定的原因(可能是hibernate找不到它的原因)和@JoinColumn在getter上。我不明白这一点。我会把ManyToOne放在场上。