2
@Entity
@Table(name = "persons")    
public class Person implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id")
        private Integer id;
        @Basic(optional = false)
        @Enumerated(EnumType.STRING)
        @Column(name = "occupation")
        private Occupation occupation;
        @Basic(optional = false)
        @Column(name = "address")
        private String address;
    }

I am trying to persist this Object and I keep getting this Exception:

Caused by: java.lang.IllegalArgumentException: Object: com.entity.Person(id[null] occupation[null] address["Toronto"] is not a known Entity type.

I tried to look up this Exception but the explanations are way to vague for me to understand. I am kind of new to JPA

Can anyone please help me.

P.S. I do have getters and setters

Thanks in advance.

john.p.doe
  • 411
  • 2
  • 10
  • 21

4 Answers4

3

Following conditions must be met so that your entities are found by the JPA provider.

In case of a web application (application runs in an application server), it is enough to mark entities with @Entity annotation, and be sure that in your persistence.xml following is NOT set: <exclude-unlisted-classes>true</exclude-unlisted-classes>

In case of standard Java SE application, you must list your classes either in persistence.xml, or referenced orm.xml.

OndroMih
  • 7,280
  • 1
  • 26
  • 44
1

put @Entity annotation before class, and generate getters and setters for all fields... Try looking for any example/tutorial.

Meriton Husaj
  • 239
  • 3
  • 9
  • 1
    Oh I do have that annotation and I do have all the getters and setters. My bad I didnt want to make the question long thats why i removed those. – john.p.doe Oct 03 '15 at 21:46
  • oh okay then, can you post your persistence.xml, for more info? In any case you're using Toplink(which is outdated) use EclipseLink, and drop all your tables before trying to run your application again. – Meriton Husaj Oct 04 '15 at 20:49
  • I upgraded to latest Eclipse link jar and it did the trick. I was surprised though. That is so weird because rest of the entities were working absolutely fine. But thanks for the help and solution. – john.p.doe Oct 05 '15 at 06:27
1

If you are trying to crate an POJO and then save it with a repository, make sure you are not creating the POJO like this:

Student student = new Student() {{ setName(xxx);setAge(xxx); }};

This statement will create an anonymous subclass of Student, and the subclass does not have the @Entity annnotation, so Hibernate can not identify.

folkboat
  • 167
  • 1
  • 7
0

If you have not added orm.xml mapping file in persistence.xml or not used @Entity.

<mapping-file>META-INF/orm.xml</mapping-file>

then add below tag in the same file or used @Entity

<exclude-unlisted-classes>false</exclude-unlisted-classes> 
Atul Jain
  • 1,035
  • 2
  • 16
  • 24