JPA persistence.xml

本文详细记录了在SE环境下使用TopLink和Hibernate的JPA实现配置,以及如何在EE环境中简化配置。讨论了两者在persistence.xml文件中的不同配置项,特别强调了SE环境限制和EE环境下的简化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前使用JPA的实现是toplink,现在改为hibernate,所以要修改persistence.xml文件,两者的配置有一些不一样,并且在EE环境下面和SE的环境下面也有不一样,还有一点,那就是当persistence.xml里面有些格式出错的时候,虽然出错的不是我们需要的那个单元,但也会使得整个persistence.xml报废。

下面帖的是在SE的环境下面使用toplink和hibernate的实现,两者都写在同一个persistence.xml里面。这样切换起来也方便一些。

<?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="TestSSH2PU" transaction-type="RESOURCE_LOCAL"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <class>com.hadeslee.jpaentity.Department</class> <class>com.hadeslee.jpaentity.Person</class> <properties> <property name="toplink.jdbc.user" value="sa"/> <property name="toplink.jdbc.password" value="hadeslee"/> <property name="toplink.jdbc.url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"/> <property name="toplink.jdbc.driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="toplink.ddl-generation" value="create-tables"/> </properties> </persistence-unit> <persistence-unit name="TestSSH1PU2" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.hadeslee.jpaentity.Department</class> <class>com.hadeslee.jpaentity.Person</class> <properties> <property name="hibernate.connection.driver_class" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="hibernate.connection.url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"></property> <property name="hibernate.connection.username" value="sa"></property> <property name="hibernate.connection.password" value="hadeslee"></property> <property name="hibernate.show_sql" value="true"></property> <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"></property> <property name="hibernate.current_session_context_class" value="thread"></property> </properties> </persistence-unit> </persistence>
在SE的环境下面,是不能使用容器的JTA的数据源的。并且不能使用
<exclude-unlisted-classes>true</exclude-unlisted-classes>这个属性。
本文重点是记录下两个常用的JPA的实现的配置。目前是在SE环境下的配置。EE环境下面的配置如下:

<?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="unit_mssql" transaction-type="JTA"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <jta-data-source>MobileOAMSSQL</jta-data-source> <properties> <property name="toplink.ddl-generation" value="create-tables"/> <property name="toplink.logging.level" value="FINE"/> </properties> </persistence-unit> <persistence-unit name="MyApp-ejbPU2" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>MobileOAMYSQL</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> </persistence>
在EE环境下面使用JPA配置就简单了许多,首先他可以把当前模块的CLASS文件都包括进来,不用手工指定。并且也少了很多有关于数据库连接的操作,因为这个时候都是从容器里面去取数据源的。并且此时的事务是由容器去管理的,也就是使用JTA,不再是RESOURCE_LOCAL了。这样在代码里面就不用em.getTransaction().begin();和em.getTransaction().commit()了,并且可以使用注入功能,把EntityManager注入到使用它的地方了。


Java EE应用中,`persistence.xml`是一个配置文件,用于描述实体管理器工厂(EntityManagerFactory)及其相关的持久化单元(Persistence Unit)。它通常位于项目的`META-INF`目录下,因为这是一个元数据文件,不直接参与应用程序的运行。 创建`persistence.xml`的步骤如下: 1. **打开`META-INF`目录(如果不存在,需要新建)**:在Java项目结构中找到`src/main/resources`目录,然后进入`META-INF`文件夹。 2. **创建新的XML文件**:右键点击`META-INF`,选择“New”->“Other”,然后选择“XML File”,输入文件名如`persistence.xml`,保存。 3. **编辑文件内容**: - 使用文本编辑器打开文件,添加以下基本结构: ```xml <persistence 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_2.xsd" version="2.2"> <persistence-unit name="your-persistence-unit-name" ...> <!-- 配置项 --> </persistence-unit> </persistence> ``` - `name`属性应该替换为你实际的持久化单元名称,这是连接数据库的关键标识符。 - 在`<persistence-unit>`标签内,你需要提供数据库连接信息、JPA提供商等详细配置,例如数据源、数据库URL、用户名、密码以及实体类路径(using-classpaths)等。 4. **指定实体类**: ```xml <class>com.example.YourEntityClass</class> ``` 将`YourEntityClass`替换为你项目中包含实体类的包名及类名。 5. **保存并关闭**:完成所有配置后,记得保存`persistence.xml`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值