I\'ve the following entity:
@Entity
public class Employee implements Serializable{
private static final long serialVersionUID = 3454567L;
@Id
@G
That come from a classloader issue, when hibernate load initial class on deployment, a dummy classLoader is created to build catalogue. When your are at runtime, the class in the catalogue doesn't have the same class loader and the method of sun.reflect.UnsafeFieldAccessorImpl.ensureObj failed.
protected void ensureObj(Object obj)
{
if(!field.getDeclaringClass().isAssignableFrom(obj.getClass()))
{
throwSetIllegalArgumentException(obj);
}
}
This method is called from DirectPropertyAccessor$DirectGetter.get(Object) line: 57
I solve the problem in glassfish 4.1 and hibernate-entitymanager-4.3.11.Final.jar by creating a custom jpa provider whose attach the right class loader.
package org.hibernate.jpa;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitInfo;
import org.hibernate.jpa.HibernatePersistenceProvider;
/**
* Lazy HibernatePersistenceProvider whose take Thread class loader as source
* and not the dummy one
*
* @author Alexandre Heroux
*
*/
public class LazyHibernatePersistenceProvider extends HibernatePersistenceProvider {
@SuppressWarnings("rawtypes")
@Override
public EntityManagerFactory createContainerEntityManagerFactory(final PersistenceUnitInfo info,
final Map properties) {
return (EntityManagerFactory) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class[] { EntityManagerFactory.class }, new InvocationHandler() {
private EntityManagerFactory entityManagerFactory = null;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(getEntityManagerFactory(info, properties), args);
}
/**
* Create the EntityManagerFactory when is not exists
*
* @param info
* @param properties
* @return
*/
protected EntityManagerFactory getEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
if (entityManagerFactory == null) {
synchronized (this) {
if (entityManagerFactory == null) {
entityManagerFactory = LazyHibernatePersistenceProvider.this
.createParentContainerEntityManagerFactory(info, properties);
}
}
}
return entityManagerFactory;
}
});
}
/**
* Call parent method
*
* @param info
* @param properties
* @return
*/
protected EntityManagerFactory createParentContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
return super.createContainerEntityManagerFactory(info, properties);
}
}
I have the same issue. This bug seems to have started with HB 4.3.6 and has survived, I am trying 4.3.10; with the same results.
It seems to me that the way entities' fields are accessed changed in HB 4.3.6+, probably related with the class loader. Prior to HB 4.3.6, embedding the DB driver jar file in the ear file works fine (very easy and clean for deployment). After 4.3.6, it doesn't work (with a class not found exception), now the driver jar file has to be put in the lib folder of Glassfish.
Juan
Your class should have the fields mapped to the table column using annotation like
@Entity
@Table(name="employee")
public class Employee implements Serializable{
private static final long serialVersionUID = 3454567L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@cloumn(name="emp_id")
private int empId;
@Column(name="user_name")
private String username;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
It could be a problem with Hibernate.
The following exception is thrown when trying to persist a new "Product" entity (see test case) by Hibernate 4.3.6.FINAL. However, it works perfectly fine if I change the maven dependency to 4.3.5.FINAL without touching any other piece of code.
From the Hibernate issue tracker
So I suggest changing Hibernate's version to 4.3.5. I had the same problem and that fixed it.