org.hibernate.AnnotationException: Unknown mappedBy in: error
Asked Answered
L

3

8

I am trying one to one relationships using annotations.
This is the code.

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;
    }


}

}

I am getting the following exception:

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)

What is wrong with my code? Can someone help?

Lindsley answered 24/8, 2013 at 12:49 Comment(3)
Please post your Address class.Ambitendency
And read the error message: "referenced property unknown: test.hibernate.Address.employee"Rilke
Yori, I have already posted the Address class. Just now separated it from Employee.Lindsley
J
20

Problem is not the value of mappedBy itself. Problem is that there is no @Entity annotation in Address class. Adding it will solve a problem. In Java SE environment class should also be added to persistence.xml.

Jeremiad answered 24/8, 2013 at 13:33 Comment(4)
Thanks Mikko..!! Your suggestion worked. But now I am facing another issue, I have edited my original post. Can you please help?Lindsley
@Aniket hey, accept the answer if it solved your original issue. Ask another question if you have any more issues. You will get bad reputation among the community if you don't.Noshow
@Aniket Changing question fully makes this site mess, because in new context answers to original questions look rather odd. Feel free to bring original question back and present new one separately.Jeremiad
Thanks Geeth and Mikko. Have posted seperate question for new problem. Here is the New questionLindsley
L
0

The same exception will occur if we don't specify the mapping class entry <mapping class="test.hibernate.Address" /> in hibernate configuration file.

Lavoie answered 19/10, 2016 at 2:46 Comment(0)
I
0

No @Entity Annotation In Address Class

In Employee Class you are already added @Entity annotation , but in the address Class you don't added @Entity annotation so your problem so clearly just add @Entity to your class to be like that :

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

The problem is no bean was created because @Entity annotation responsible about that like @Service and/or @Component annotations .

Interment answered 9/3, 2021 at 9:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.