java - 映射通过引用未知的目标实体属性 - hibernate 错误 maven 注释

标签 java hibernate annotations

我正在开发一个学习 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)类 HibernateUtil { 私有(private)静态最终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

最佳答案

@OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
private List<Question> _questions ;

因此,您告诉 Hibernate:查看 Question.survey 字段以了解此关联是如何映射的。

问题中是否有名为调查的字段?不,没有。该字段名为 _survey

请让您的代码可读,让您的生活更轻松,并尊重 Java 命名约定。变量不以下划线开头。您真的不希望应用程序的每个 JPQL/HQL 查询到处都有下划线。

关于java - 映射通过引用未知的目标实体属性 - hibernate 错误 maven 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25779647/

相关文章:

java - 我的应用程序的用户界面存在多个问题

java - BatchUpdateException updateCounts 始终为 1

java - 无法初始化类org.springframework.aop.framework.DefaultAopProxyFactory

java - JPA + hibernate : How to define a constraint having ON DELETE CASCADE

java - 哪个方面/拦截器处理@Transactional注释

java - 根据 JAX-RS 中的规范使用带有 @PathParam 的 PathSegment?

java - 如何在 JSP 中打开 PDF 文件

sql - Postgresql - 将列类型从 oid 更改为 bytea 并保留数据

java - 如何创建和使用具有 Spring+JUnit 测试配置注释的自定义注释?

java - 如何防止一个类中出现多个 Java 注解实例