【问题标题】:org.hibernate.AnnotationException: Unknown mappedBy in: errororg.hibernate.AnnotationException: Unknown mappedBy in: error
【发布时间】:2013-08-24 12:49:35
【问题描述】:

我正在尝试使用注释的一对一关系。
这是代码。

package test.hibernate;

@Entity
@Table(name="EMPLOYEE")

public class Employee {
    @Id
    @GeneratedValue
    @Column(name="EMP_ID")
    private Long empID;

    @Column(name="fName")
    private String fName;

    @Column(name="lName")
    private String lName;

    @OneToOne(mappedBy="employee", cascade=CascadeType.ALL)
    private Address address;


    public Employee(/* Long empID, */String fName, String lName) {
        super();
        // this.empID = empID;
        this.fName = fName;
        this.lName = lName;
    }

    public Employee() {
        super();
    }

    public Long getEmpID() {
        return empID;
    }

    public void setEmpID(Long empID) {
        this.empID = empID;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}


package test.hibernate;

@NamedNativeQueries({ @NamedNativeQuery(name = "addressQuery", query = "from Address", resultClass = Address.class) })

public class Address {

    @Column(name = "EMP_ID")
    @GenericGenerator(name = "gen", strategy = "foreign", parameters = @Parameter(name = "property", value = "employee"))
    private Long empID;

    @Column(name = "ADDRESS")
    private String address;

    @Column(name = "CITY")
    private String city;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Employee employee;

    public Address() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Address(/* Long empID, */String address, String city) {
        super();
        // this.empID = empID;
        this.address = address;
        this.city = city;
    }

    public Long getEmpID() {
        return empID;
    }

    public void setEmpID(Long empID) {
        this.empID = empID;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }


}

}

我收到以下异常:

Hibernate one to many (XML Mapping)
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.AnnotationException: Unknown mappedBy in: test.hibernate.Employee.address, referenced property unknown: test.hibernate.Address.employee
    at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:129)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1127)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
    at test.hibernate.HibernateUtil.openSession(HibernateUtil.java:14)
    at test.hibernate.Test.main(Test.java:11)
Exception in thread "main" java.lang.NullPointerException
    at test.hibernate.HibernateUtil.openSession(HibernateUtil.java:19)
    at test.hibernate.Test.main(Test.java:11)

我的代码有什么问题?有人可以帮忙吗?

【问题讨论】:

  • 请发布您的地址类。
  • 并阅读错误信息:“referenced property unknown: test.hibernate.Address.employee”
  • Yori,我已经发布了 Address 类。刚刚将它与 Employee 分开。

标签: hibernate hibernate-annotations


【解决方案1】:

问题不在于mappedBy 本身的值。问题是Address 类中没有@Entity 注释。添加它会解决一个问题。在 Java SE 环境中类也应该添加到 persistence.xml 中。

【讨论】:

  • 谢谢 Mikko..!!你的建议奏效了。但现在我面临另一个问题,我已经编辑了我的原始帖子。你能帮忙吗?
  • @Aniket 嘿,如果它解决了您的原始问题,请接受答案。如果您还有其他问题,请提出另一个问题。如果你不这样做,你会在社区中获得坏名声。
  • @Aniket 不断变化的问题让这个网站变得一团糟,因为在新的上下文中,对原始问题的回答看起来很奇怪。随意将原始问题带回并单独提出新问题。
  • 感谢 Geeth 和 Mikko。已针对新问题发布了单独的问题。这是New question
【解决方案2】:

如果我们没有在hibernate配置文件中指定映射类入口<mapping class="test.hibernate.Address" />,也会出现同样的异常。

【讨论】:

    【解决方案3】:

    地址类中没有@Entity注解

    Employee 类中,您已经添加了@Entity 注释,但是在地址类中您没有添加@Entity 注释,所以您的问题很明显只需将@Entity 添加到您的类中即可:

    @Entity
    public class Address {
      // your code here ....
    }
    

    问题是没有创建 bean,因为 @Entity 注释负责像 @Service 和/或 @Component 注释。

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2018-07-24
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多