Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 418 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JPA: java.lang.IllegalArgumentException: Not an entity

#1
I wrote a simple Java that uses POJOs, servlets, JSPs. I am using:

- Tomcat 7.0.16
- Oracle 11g
- Oracle Java build 1.6.0_26-b03

I use ivy to resolve my dependencies:

<dependency org="oracle" name="ojdbc6" rev="11g" conf="default -> default" />

<dependency org="org.hibernate" name="hibernate-commons-annotations" rev="3.2.0.Final" conf="compile -> runtime" />
<dependency org="org.hibernate" name="hibernate-annotations" rev="3.5.6-Final" conf="default -> runtime" />
<dependency org="org.hibernate" name="hibernate-core" rev="3.6.5.Final" conf="default -> runtime" />
<dependency org="org.hibernate" name="hibernate-validator" rev="4.0.2.GA" conf="runtime -> runtime" />
<dependency org="org.hibernate.javax.persistence" name="hibernate-jpa-2.0-api" rev="1.0.1.Final" conf="compile -> default" />
<dependency org="org.hibernate" name="hibernate-entitymanager" rev="3.6.5.Final" conf="compile -> default" />

My POJO Vehicle contains the following annotations:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;

@Entity
public class Vehicle implements Serializable{


/**
*
*/
private static final long serialVersionUID = 8560417193012227968L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long identifier;

private String serialNumber;
private String model;
private String manufacturer;
private int year;
private String condition;

@Version
private long version;

public Vehicle(){
}

When I run my code, I get the following:


2013-10-28 11:40:50 ERROR SqlVehicleService:42 - java.lang.IllegalArgumentException: Not an entity: class com.foo.demos.car.model.Vehicle
java.lang.IllegalArgumentException: Not an entity: class com.foo.demos.car.model.Vehicle
at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160)
at org.hibernate.ejb.criteria.QueryStructure.from(QueryStructure.java:138)
at org.hibernate.ejb.criteria.CriteriaQueryImpl.from(CriteriaQueryImpl.java:179)
at com.foo.demos.car.impl.SqlVehicleService.getVehicles(SqlVehicleService.java:37)
at org.apache.jsp.secured.vehicles_jsp._jspService(vehicles_jsp.java:194)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:572)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

My `persistence.xml` does not contain any information about Vehicle. My understanding is that annotations are enough. Is that correct?

<?xml version="1.0" encoding="UTF-8"?>
<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

[To see links please register here]

;
version="1.0">

<persistence-unit name="cardemo">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- the JNDI data source -->
<non-jta-data-source>java:comp/env/jdbc/cardemo</non-jta-data-source>
<properties>
<property name="javax.persistence.validation.mode" value="NONE" />
<!-- if this is true, hibernate will print (to stdout) the SQL it executes,
so you can check it to ensure it"s not doing anything crazy -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<!-- since most database servers have slightly different versions of the
SQL, Hibernate needs you to choose a dialect so it knows the subtleties of
talking to that server -->
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<!-- this tell Hibernate to update the DDL when it starts, very useful
for development, dangerous in production -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>


I obviously overlooked something. What's that? What am I missing?
Reply

#2
> My persistence.xml does not contain any information about Vehicle. My understanding is that annotations are enough. Is that correct?

No, it's not. The entities must be listed in the persistence.xml file, under the persistence-unit element:

<class>com.foo.demos.car.model.Vehicle</class>
Reply

#3
You need to give a hint to where the persistent entities can be found. I normally configure via Spring so don't have the exact info required however look here for some further discussion.


[To see links please register here]

Reply

#4
you can specify your entity locations via `<property name="packagesToScan" value="" />` in the persistence.xml
Reply

#5
If you **dynamically** build your persistence and add properties, you can **declare entity classes** using `'org.reflections:reflections:0.9.11'`:

new HibernatePersistenceProvider()
.createContainerEntityManagerFactory(
new javax.persistence.spi.PersistenceUnitInfoImpl(persistenceUnitName),
ImmutableMap.<String, Object>builder()
.put(AvailableSettings.LOADED_CLASSES, getEntities())
.build());

...


private List<Class<?>> getEntities() {

Set<Class<?>> entitiesInPackage = new Reflections("rf.dom.model")
.getTypesAnnotatedWith(Entity.class); // Annotated @Entity

List entities = new ArrayList<>(entitiesInPackage);

return entities;
}
Reply

#6
My problem was that I created a `SessionFactory` like this:

```
Configuration configuration = new Configuration();
configuration.configure();

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
```
instead of like this:
```
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
```
When I replaced the former with the later, it worked! So be careful when using `SessionFactory`'s initialization from tutorials.
Reply

#7
Add **packages-to-scan** entry in your yml file.

jpa:
default:
packages-to-scan:
- 'com.foo.demos.car.model'
- 'your.entity.package.name'
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through