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!