日期:2014-05-20  浏览次数:20948 次

在J2SE中使用JPA 访问EntityManager遇到的问题记录
public class Main {
	public static void main(String[] args) {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("HelloTitan");
		EntityManager em = factory.createEntityManager();
		
		Cabin c = new Cabin();
		c.setId(0);
		c.setBedCount(50);
		c.setDeckLevel(2);
		c.setName("CabinOne");
		c.setShipId(0);
		
		em.persist(c);
		System.out.println( em.contains(c) );
		Cabin b = em.find(Cabin.class, 0);
		System.out.println(b.getName());
		
		em.close();
		factory.close();
	}

}

?

问题一:?

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named HelloTitan
?at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
?at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
?at Main.main(Main.java:10)

?

原因:没有Persistence provider

解决办法:将需要的lib添加到classpath中,Jboss使用的是hibernate实现,这些lib在下面的目录中能找到

jboss-5.0.0.GA\common\lib\

?

hibernate-annotations.jar

hibernate-commons-annotations.jar

hibernate-core.jar

hibernate-entitymanager.jar

hibernate-jmx.jar

hibernate-validation.jar

且还依赖 commons-collections.jar

?

问题二:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: HelloTitan] Unable to build EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
	at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
	at Main.main(Main.java:10)
Caused by: org.hibernate.HibernateException: Could not find datasource
	at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
	at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
	at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
	... 4 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
	at javax.naming.InitialContext.lookup(InitialContext.java:392)
	at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
	... 11 more

?

原因:JNDI没有初始化成功

解决办法:?在classpath添加 jnid.properties 文件 (以JBoss为例,配置如下)

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

?

?

问题三:

程序成功运行,但是数据没有写入数据库

原因:没有提交数据

解决办法:em.getTransaction().commit();

?

?

?

?其他相关记录:

?

?J2SE环境中使用JPA的(Hibernate+MySQL)persistence.xml例子

?如果想使用JNDI的数据源,可以将 non-jta-data-source 这一段的注释去掉,并且注意JNDI的MySqlDS是可访问的!

?

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
	<persistence-unit name="EntityRelationships"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		
		<!--
		<non-jta-data-source>MySqlDS</non-jta-data-source>
		-->
		<class>cn.heapstack.jpa.entityrelationships.domain.Customer</class>
		<class>cn.heapstack.jpa.entityrelationships.domain.Address</class>
		<class>cn.heapstack.jpa.entityrelationships.domain.CreditCard</class>
		<class>cn.heapstack.jpa.entityrelationships.domain.Phone</class>
		<class>cn.heapstack.jpa.entityrelationships.domain.Ship</class>
		<class>cn.heapstack.jpa.entityrelationships.domain.Cruise</class>
		<properties>
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/ejb3" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="root000" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
			<property name="hibernate.show_sql" value="true" />
			<!-- 
			<property name="hibernate.format_sql" value="true" />
			 -->
		</properties>
	</persistence-unit>
</persistence>

?

?

如何在JBoss里配置MySqlDS 数据源?

?

?找到这个文件,并且修改 jboss-5.0.0.GA\docs\examples\jca\mysql-ds.xml

?修改完成之后放到 jboss-5.0.0.GA\server\default\deploy 目录下,重启JBoss就可以了

?同时请参看我的另一篇文章: 使用JNDI查找JBOSS内配置好的数据源遇到的问题 ...

?

?

-----------------------------------------------------

?

?

?