我是JPA的新手。早些时候我知道Hibernate。但是根据要求,我需要增强Hibernate + JPA配置。
请查看我的配置。
POJO:
package com.skc.order;
import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="blc_order")
public class Orders {
@Id
@Column(name="order_id")
Long orderId;
@Column(name="ORDER_NUMBER")
String orderNumber;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
}
Persistance.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="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
<class>com.skc.order.Orders</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/broadleaf" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.username" value="mysql" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
EntityManagerTest类:
package com.skc.order;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class EntityManagerTest {
/*@PersistenceContext(unitName = "ExpensePersistentUnit")
protected EntityManager em;*/
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ExpensePersistentUnit");
EntityManager em = emf.createEntityManager();
Query qry = em.createNativeQuery("select * from blc_order", Orders.class);
Orders order = em.find(Orders.class, 1512);
System.out.println(order.getOrderNumber());
}
}
在ClassPath中添加了jar:
antlr-2.7.7
dom4j-1.6.1
hibernate-commons-annotations-4.0.1.Final
hibernate-core-4.2.0.Final
hibernate-jpa-2.0-api-1.0.1.Final
hibernate-validator-4.3.1.Final
javassist-3.15.0-GA
jboss-logging-3.1.2.GA
jboss-logmanager-1.4.0.Final
jboss-transaction-api_1.1_spec-1.0.1.Final
log4j-jboss-logmanager-1.0.1.Final
slf4j-api-1.6.1
slf4j-log4j12-1.6.1
commons-logging-1.1.1
hibernate-3.2.3.ga
hibernate-entitymanager-3.3.2.GA
我得到例外:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/cfg/SettingsFactory;)V from class org.hibernate.ejb.Ejb3Configuration
at org.hibernate.ejb.Ejb3Configuration.<init>(Ejb3Configuration.java:134)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:124)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.skc.order.EntityManagerTest.main(EntityManagerTest.java:13)
请帮我解决这个问题。 谢谢......
答案 0 :(得分:1)
对于Hibernate-core使用3.6.3版本..
由于类版本不匹配导致此错误。所以几分钟后我也得到了同样的错误。我刚刚更改了版本,错误现在消失了。我正在使用hibernate核心3.6.3.FINAL与spring版本4.1.7.RELEASE。希望这也适合你。如果您有疑问请发表评论。