在JavaSE中获得EntityManagerFactory测试 EJB组件

在网上找了不少java se中获得enetityManagerFactory的方法,很多都讲的很简略,不知道是为何,总让人看不太明白。我就写个完整的吧。

 

 

1、在java se项目中的persistence.xml中添加如下代码:

<persistence-unit name="oracle_test" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<!-- 
       	<class>org.jenfer.jpa.entity.Emp</class>
       	 -->
        <properties>
           <property name = "hibernate.connection.driver_class" value ="oracle.jdbc.driver.OracleDriver"/>
           <property name = "hibernate.connection.url" value = "jdbc:oracle:thin:@218.198.177.109:1521:orcl"/>
           <property name = "hibernate.dialect" value = "org.hibernate.dialect.OracleDialect"/>
           <property name = "hibernate.connection.username" value = "iping123"/>
           <property name = "hibernate.connection.password" value = "iping123"/>
           <property name = "hibernate.hbm2ddl.auto" value="update"/>
           <property name="hibernate.show_sql" value="true"/>
        </properties>
	</persistence-unit>

可以和其他persistence-unit共存

 

2、由于是用java se访问jni来获得这个oracle_test的,所以要在java se类路径下添加:

jndi.properties:

# DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
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

 当然,上面的内容你也可以通过硬编码的方式写到程序里。

 

3、下面就是客户端调用了,很简单:

package test;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class UserTest{
	private EntityManagerFactory emf=
				Persistence.createEntityManagerFactory("oracle_test");;
	
	public EntityManagerFactory getEmf() {
		return emf;
	}

	public static void main(String[] args) {
		UserTest ut= new UserTest();
		System.out.println(ut.getEmf());
	}

}

你可能感兴趣的:(java,oracle,Hibernate,ejb,jpa)