JPA Entity not mapped exception - java

I am using JPA and java as technology and writing very simple web application.
There is one class called Employee, look like below code
#Entity
#Table(name="Employee")
public class Employee{
#Id
private int id;
#Column(name="name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
one table called Employee is exist into database.
Its persistence.xml file looks like below code.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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_2_0.xsd">
<persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- Entity classes -->
<class>com.solution.domain.Employee</class>
<properties>
<!-- The JDBC driver of your database -->
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<!-- The JDBC URL to the database instance -->
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
<!-- The database username -->
<property name="javax.persistence.jdbc.user" value="postgres" />
<!-- The database password -->
<property name="javax.persistence.jdbc.password" value="postgres"/>
<property name="javax.persistence.jdbc.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
</properties>
</persistence-unit>
</persistence>
Employee class entry exist in this xml file.
There are not issue while startup of application. Every thing working fine.
But when query is executed then it throw and exception.
"Employee is not mapped [select e from Employee e]"
EntityManager em = getEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("select e from Employee e");
List<Employee> employees = query.getResultList();
if(employees != null){
return employees.get(0);
}
em.getTransaction().commit();
I am unable to understand where mapping is missing. As i already mentioned its a web project. So for more clarification i am attaching one screen shot of my project too.
Thanks in advance.

I got the problem. It was an silly mistake. I was using JPA but in Employee class I used the Entity annotation from hibernate implementation instead of JPA package.

Side note: It's a bad idea to model the database with JPA, e.g., by implementing the integer surrogate key in the data model. JPA is supposed to help persist an object model, which would use the natural "key" to distinguish objects. A JPA entity should only expose attributes of the object model. This has the benefit of greatly simplifying the expression of relationships because you relate objects, not ID fields. Don't use JPA for database translation, use it for object persistence.

Related

Wildfly - how to enable transactions to enable lazy loading

I have User and Post entities with a unidirectional relationship. I am getting org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: exception when I try to get all posts from a specific user.
According to these SO answers, the optimal way to handle this is to use #Transactional annotation for the service method/class. Placing annotations does not work for me. I am using Wildfly server, Hibernate, MySQL, and Java EE MVC web framework.
How do I make it work, i.e. get the posts from a user? I managed to do it via eager loading, but this is not recommended for performance reasons.
#Transactional
public class UserService {
private List<User> users;
private Set<Post> posts;
#PersistenceContext(unitName = "my-pu")
private EntityManager em;
public List<User> getUsers() {
String qlQuery = "SELECT u FROM User u";
Query query = em.createQuery(qlQuery);
users = query.getResultList();
return users;
}
#Transactional
public Set<Post> getUserPosts(Long userId) {
String qlQuery = "SELECT u FROM User u WHERE u.id = :userId";
Query query = em.createQuery(qlQuery);
query.setParameter("userId", userId);
User user = (User) query.getSingleResult();
posts = user.getPosts();
return posts;
}
}
This is my Service method.
#Path("users")
#Controller
public class UserController {
#Inject
private Models models;
#Inject
private UserService service;
#GET
#Produces("text/html")
#View("showUsers.ftl")
public void users() {
List<User> users = service.getUsers();
models.put("users", users);
}
#GET
#Path("{id}")
#Produces("text/html")
#View("showUserPosts.ftl")
public void getPosts(#PathParam("id") Long userId) {
System.out.println("here am i");
Set<Post> posts = service.getUserPosts(userId);
models.put("posts", posts);
}
}
This is my controller.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="my-pu" transaction-type="JPA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testdb?useSSL=false"/>
<property name="javax.persistence.jdbc.user" value="testuser"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="test623"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.dialect.storage_engine" value="innodb"/>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="javax.persistence.sql-load-script-source"
value="META-INF/sql/data.sql" />
</properties>
</persistence-unit>
</persistence>
An this is my persistence unit.
Error message:
org.jboss.resteasy.spi.UnhandledException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.zetcode.model.User.posts, could not initialize proxy - no Session
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:257)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:195)
at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:539)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:461)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:231)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:137)
at org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:361)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:140)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:217)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:227)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:67)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
You are returning an uninitialized collection proxy out of session boundaries (demarcated by the transaction in this case).
You can initialize the proxy by calling Hibernate.initialize(posts) or by just calling posts.size() before returning it (the first approach more clearly describes the intent imo).
An alternative that I use most often is to utilize DTOs, so that I don't have to deal with detached objects, but also because of the ability to tailor presentation objects (and web service responses in general) to the exact needs of clients using them. Also that way the domain model (Hibernate entities) is decoupled from the presentation logic, allowing the two to evolve independently.
Other alternatives include Open Session in View (anti-?) pattern and hibernate.enable_lazy_load_no_trans property, but they are less flexible and have their own pros and cons.

Error with persistence using EclipseLink and UCanAccess

I am trying to develop an app for exercise reasons. I am using MSAccess 2010 as the database with UCanAccess (3.06) as the driver and the EclipseLink 2.1 as the entity framework.
I am stuck in adding new records to the database. Here the error code:
Internal Exception: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 user lacks privilege or object not found: IDENTITY_VAL_LOCAL
Error Code: -5501
Call: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
Query: ValueReadQuery(name="SEQ_GEN_IDENTITY" sql="SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1")
It seems to me that the autogenerate of the id fails. The entity class was generated vie Netbeans and looks like this:
#Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "ID")
private Integer id;
By default, EclipseLink tries to automatically detect the underlying database and generate SQL statements using the appropriate SQL dialect. That apparently isn't working for you because the SQL statement to retrieve the last created identity value is not recognized by UCanAccess .
You could try adding a target-database directive to your EclipseLink configuration specifying SQLServer in an attempt to get a working SQL statement (SELECT ##IDENTITY) to retrieve the last created ID value. However, bear in mind that there are significant differences between T-SQL and Access SQL so you will probably continue to encounter other compatibility issues between EclipseLink and UCanAccess.
before knowing above answer i was also facing same problem for inserting new record in access Database ,
Thanks to Mr. Gord Thompson to give a great Solution for me ,
and it is working too.
i have just added one line in my persistence.xml file..
property name="eclipselink.target-database" value="HSQL"
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="OnePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>design_frames.One</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:ucanaccess://C:\One\One.accdb"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.driver" value="net.ucanaccess.jdbc.UcanaccessDriver"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.target-database" value="HSQL"/>
</properties>
</persistence-unit>
</persistence>

How to use EntityManager in Singleton DAO from Servies

I'm new in JavaEE. I'm trying to make webapp using JPA with DAO and Service layer. Part of the task is not to use Spring and Hibernate. So i can use only JPA.
As i understand, Service and Dao should be a Singletones. I should get EntityManager on each database operation, and close it after. I'm using GenericDao for some operation, and EntityManager is needed in Dao, as a field.
How to use it? How can i get entityManager in Service, start transaction in service, and pass it inside the DAO implementation?
Here's example of my DAO interface.
public interface GenericDAO<T> {
void save(T entity);
void merge(T entity);
void delete(T entity);
...
}
I can get EntityManager in Service. In implementation all these method's will be using EntityManager. So i need it there as a field.
So how can i pass EntityManager in my DAO implementation and still make all this construction thread safe?
I don't think make inerface method like this is a good idea:
void save(T entity, EntityManager entityManager);
void merge(T entity, EntityManager entityManager);
EntityManager without Hibernate? What will make the ORM to it? Maybee you should use JDBC in the DAL instead of the EntityManager without Hibernate.
Using only JPA is a good goal. Spring has been around a long time and has a very vendor specific implementation of data repositories. Hibernate is much closer to JPA (in my opinion), but is also quite specific. The EntityManager is effectively a generic DAO as you describe it. It has persist, merge, find, and delete methods, as well as many others.
In order to learn JPA a bit you can create a simple project with a simple entity and go from there. I have many such "playground" projects, but my simplest one is courtesy of Geoffroy Warin
A main App class:
public class App {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
EntityManager em = emf.createEntityManager();
try {
EntityTransaction tx = em.getTransaction();
tx.begin();
User user = new User();
em.persist(user);
tx.commit();
System.out.println("user=" + user);
User found = em.find(User.class, 1L);
System.out.println("found=" + found);
} finally {
emf.close();
}
}
}
A simple User entity:
#Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
public Long getId() { return id; }
#Override
public String toString() {
return "User:"+id;
}
}
and a persistence.xml (goes in resources/META-INF directory):
<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_2_0.xsd"
version="2.0">
<persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>model.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:standalone" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
And, of course, a POM file to bring in all the dependencies.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jpa-playground</groupId>
<artifactId>jpa-playground</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
</dependencies>
</project>
Figure out how to put all this together and you can play with JPA to your hearts content. Note that is uses Hibernate as the JPA provider, but it does not use any hibernate specific constructs.
Creating a service layer is usually done with EJB's, e.g., stateless and statefull beans, and for that you need a container like Wildfly or Tomcat EE, among others. However, you can simulate them by creating a class called ServiceLayer and putting methods like createUser, changeUserName, removeUser and things like that in it. It would use the EntityManager on behalf of the application.

Hibernate with Sql Server fail for nvarchar field with "No Dialect mapping..."

I'm using Hibernate's JPA-Implementation to access our SQL Server 2012 database.
When trying to select a nvarchar field in a native query, I get an exception "No Dialect mapping for JDBC type: -9".
It looks much like No Dialect mapping for JDBC type: -9 with Hibernate 4 and SQL Server 2012 or No Dialect mapping for JDBC type: -9 but I couldn't find a solution for me there (both are not using JPA).
My database setup:
CREATE TABLE NvarcharExample(
exampleField nvarchar(20) PRIMARY KEY
)
INSERT INTO NvarcharExample(exampleField) VALUES ('hello')
My code:
import java.io.IOException;
import javax.persistence.*;
#Entity
class NvarcharExample {
#Id
public String exampleField;
}
public class NvarcharTest {
public static void main(String[] args) throws IOException, InterruptedException {
String queryString = "SELECT e.exampleField FROM NvarcharExample e";
// establish connection
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistenceUnit");
try {
EntityManager entityManager = entityManagerFactory.createEntityManager();
// access data using JPQL
entityManager.createQuery(queryString).getResultList(); // works
// access data using SQL (native query)
entityManager.createNativeQuery(queryString).getResultList(); // fails
} finally {
entityManagerFactory.close();
}
}
}
My persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="persistenceUnit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<!-- database connection settings -->
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://<servername>:<port>;databaseName=<databasename>" />
<property name="javax.persistence.jdbc.user" value="<user>" />
<property name="javax.persistence.jdbc.password" value="<password>" />
</properties>
</persistence-unit>
</persistence>
With sql logging enable, I get this output in my console
select nvarcharex0_.exampleField as col_0_0_ from NvarcharExample nvarcharex0_
SELECT e.exampleField FROM NvarcharExample e
I'm using
hibernate-core-4.3.10.Final.jar
hibernate-entitymanager-4.3.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-commons-annotations-4.0.5.Final.jar
sqljdbc41.jar
What I've tried:
using a varchar instead of nvarchar makes it work, but I need nvarchar
using jpql instead of sql works (see my example code), but I need a native query
I tried sqljdbc4.jar in Version 4.0 and 4.1 and I tried sqljdbc41.jar
I head about subclassing the SQL Server Dialect class, but did not have any success with that
I added <property name="dialect" value="org.hibernate.dialect.SQLServerDialect" /> to my persistence.xml (right behind the password property)
I added <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" /> to my persistence.xml
I changed the persistence provider to <provider>org.hibernate.ejb.HibernatePersistence</provider>
Using #Nationalized attribute helped me to map String to nvarchar for MS SQL 2012 without dialect subclassing.
At the same time setting the hibernate.use_nationalized_character_data property to true did not worked for me.
For futher information watch docs National Character Types.
I was able to resolve that issue by subclassing the SQLServerDialect:
package packagename;
import java.sql.Types;
public class SqlServerDialectWithNvarchar extends org.hibernate.dialect.SQLServerDialect {
public SqlServerDialectWithNvarchar() {
registerHibernateType(Types.NVARCHAR, 4000, "string");
}
}
and referencing it in my persistence.xml:
<property name="hibernate.dialect" value="packagename.SqlServerDialectWithNvarchar" />
PS: It seems to be fixed with hibernate 5.1 according to this ticket: https://hibernate.atlassian.net/browse/HHH-10183

OpenJPA L2 Cache not caching related objects

I have a simple data model which consists of a parent object with a one-to-many relationship to a list of child objects.
#Entity
public class Client implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToMany(cascade = CascadeType.ALL)
private List<Session> sessions;
...
}
#Entity
public class Session implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(length = 200)
private String location;
...
}
I have enabled L2 caching using OpenJpa in the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="puOpenJPA_Gym" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>java:jboss/datasources/GymTracker</jta-data-source>
<class>za.co.blacklemon.entities.Client</class>
<class>za.co.blacklemon.entities.Session</class>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="jboss.as.jpa.providerModule" value="org.apache.openjpa"/>
<property name="openjpa.DynamicEnhancementAgent" value="false"/>
<property name="openjpa.Log" value="File=stdout, DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.QueryCache" value="true(CacheSize=10000, SoftReferenceSize=1000)"/>
<property name="openjpa.DataCache" value="true(CacheSize=20000, SoftReferenceSize=1000)"/>
</properties>
</persistence-unit>
</persistence>
I then get individual clients by calling find and then accessing the list of sessions. The second line here triggers OpenJPA to populate the child objects from the DB:
Client client = dao.find(Client.class, id);
System.out.println(client.getSessions().size());
The first time this runs there appears two queries in the log, one for the parent object and one each for any children:
executing prepstmnt 782097742 SELECT t0.contactNumber, t0.description, t0.firstName, t0.photo, t0.surname FROM Client t0 WHERE t0.id = ? [params=?]
executing prepstmnt 626498798 SELECT t1.id, t1.location FROM Client_Session t0 INNER JOIN Session t1 ON t0.SESSIONS_ID = t1.id WHERE t0.CLIENT_ID = ? [params=?]
The second time this runs the initial query is gone, as the object is retrieved from the cache, but the second (and possibly more) query is still executed.
Why does OpenJPA not store the child objects in a one-to-many relationship in the cache?
I'm using OpenJPA 2.3.0 on Java 7.
Why does OpenJPA not store the child objects in a one-to-many relationship in the cache?
OpenJPA stores the child objects, but it doesn't store the FK back into the parent. I fixed this problem in OpenJPA trunk(2.4.x) via OPENJPA-2285.

Categories