-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Unknown Entity error
PostPosted: Wed Mar 04, 2009 2:26 pm 
Newbie

Joined: Wed Mar 04, 2009 2:01 pm
Posts: 3
Hello,

I'm new to JPA and coding my first example. Unfortunately, I get the following error:
Quote:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: jpatest1.Car
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:223)
at jpatest1.JPATest1.main(JPATest1.java:24)


I have read that this error occurs, when the persistent class is not declared in the persistence.xml file, but I did declare it there.

My persistence.xml looks like this:

Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">

   <persistence-unit name="cars">
       <class>jpatest1.Car</class>
       <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/cardb" />
            <property name="hibernate.connection.username" value="xxx" />
            <property name="hibernate.connection.password" value="xxx" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
      </properties>
   </persistence-unit>

</persistence>


The persistant class:

Code:
@Entity
public class Car implements Serializable
{
    private int id;
    private String brand;
    private String model;
    private String color;

    public String getBrand()
    {
        return brand;
    }

    public void setBrand(String brand)
    {
        this.brand = brand;
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getModel()
    {
        return model;
    }

    public void setModel(String model)
    {
        this.model = model;
    }
}


My Testprogram:
Code:
public class JPATest1
{

    public static void main(String[] args)
    {
        Car car = new Car();
        car.setBrand("BMW");
        car.setModel("318d");
        car.setColor("black");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("cars");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(car);
        em.getTransaction().commit();
        em.clear();
        emf.close();
    }
}


Hibernate version is 3.3.1.
Hibernate Entity Manager and Hibernate Annotations version is 3.4.0.
Database: MySQL version 5

Hope, that someone can help me! Thank's in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 2:29 pm 
Newbie

Joined: Tue Mar 25, 2008 2:04 pm
Posts: 4
What is your database table name?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 2:38 pm 
Newbie

Joined: Wed Mar 04, 2009 2:01 pm
Posts: 3
The table name is "car". I have also tried this:

Code:
@Entity
@Table(name="car")
public class Car implements Serializable


Unfortunately, the error is the same.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 3:00 pm 
Newbie

Joined: Wed Mar 04, 2009 2:01 pm
Posts: 3
I just solved the problem. The import of the Entity annotation was wrong. I accidentally imported "org.hibernate.annotations.Entity" instead of "javax.persistence.Entity". This caused the problem.

Thank's to everybody who tried to help me!


Top
 Profile  
 
 Post subject: Re: Unknown Entity error
PostPosted: Fri Jun 01, 2012 9:29 am 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
Great thanks!
Same error here lol

Saved me a lot of time


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.