我正在研究一个测试项目来学习hibernate,不幸的是我收到了这个错误并查看了其他类似的错误,但是关注它们并没有解决我的问题。我仍然收到此错误。
有人可以检查出了什么问题,我真的很想知道我的错误是什么。
我的模型类:
@Entity
@Table(name="survey")
public class Survey implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "survey_id")
private Long _id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
private List<Question> _questions ;
@Column(name="name")
private String _name;
public Survey() {
super();
}
public Survey(Long id, List<Question> questions, String name) {
super();
_id = id;
_questions = questions;
_name = name;
Assert.notNull(_id, "_id cannot be null");
Assert.notNull(_questions, "_questions cannot be null");
Assert.notNull(_name, "_name cannot be null");
}
public Long getId() {
return _id;
}
public void setId(Long id) {
_id = id;
}
public List<Question> getQuestions() {
return _questions;
}
public void setQuestions(List<Question> questions) {
_questions = questions;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
@Override
public String toString() {
return "Survey [_id=" + _id + ", _questions=" + _questions + ", _name="
+ _name + "]";
}
}
这是第二个模型类:
@Entity
@Table(name="question")
public class Question implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private Long _id;
@Column(name = "label")
private String _label;
@Column(name="type")
private QuestionType _type;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="survey_id")
private Survey _survey;
public Question() {
}
public Question(final Long id, final String label, final QuestionType type, final Survey survey,final Long surveyId) {
_id = id;
_label = label;
_type = type;
_survey = survey;
Assert.notNull(_id, "_id cannot be null");
Assert.notNull(_label, "_label cannot be null");
Assert.notNull(_type, "_type cannot be null");
Assert.notNull(_survey, "_survey cannot be null");
}
public Long getId() {
return _id;
}
public void setId(Long id) {
_id = id;
}
public String getLabel() {
return _label;
}
public void setLabel(String label) {
_label = label;
}
public QuestionType getType() {
return _type;
}
public void setType(QuestionType type) {
_type = type;
}
public Survey getSurvey() {
return _survey;
}
public void setSurvey(Survey survey) {
_survey = survey;
}
@Override
public String toString() {
return "Question [_id=" + _id + ", _label=" + _label + ", _type="
+ _type + "]";
}
}
这是我的主要内容:
public class Application {
public static void main(String[] args) {
System.out.println("Hibernate one to many (Annotation)");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Survey survey = new Survey();
survey.setName("Ice Cream final");
session.save(survey);
Question question1 = new Question();
question1.setLabel("Whats your favorite Ice Cream");
question1.setType(QuestionType.TEXT);
question1.setSurvey(survey);
survey.getQuestions().add(question1);
session.save(question1);
session.getTransaction().commit();
System.out.println("Done");
}
这是我的hibernate util类:
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
这是我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/survey</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping class="xxxxx.Survey" />
<mapping class="xxxxx.Question" />
</session-factory>
错误:
Initial SessionFactory creation failed.org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: xxxx.model.Question.Survey in xxxx.model.Survey._questions
Exception in thread "main" java.lang.ExceptionInInitializerError
at xxxx.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
at xxxx.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
at xxxx.Application.main(Application.java:25)
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: xxxx.model.Question.SurveyModel in xxxx.model.Survey._questions
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:655)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:619)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1221)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:383)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at xxxx.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
... 2 more
答案 0 :(得分:5)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
private List<Question> _questions ;
所以你要告诉Hibernate:去查看Question.survey
字段,了解这种关联是如何映射的。
问题中是否有一个名为survey
的字段?不,没有。该字段名为_survey
。
请请,让您的代码可读,让您的生活更轻松,并尊重Java命名约定。变量不以下划线开头。你真的不希望你的应用的每个JPQL / HQL查询到处都有下划线。
答案 1 :(得分:0)
映射必须是子类的getter名称。
@OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
private List<Question> _questions ;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="survey_id")
private Survey survey;
答案 2 :(得分:0)
在问题实体类中,您必须对Survey类中的mappedBy中使用的字段使用相同的名称。所以:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
private List<Question> _questions ;
@JoinColumn(name="survey_id")
private Survey survey;