如何为Hibernate JPA创建一个persistence.xml文件? [英] How do I create a persistence.xml file for Hibernate JPA?

查看:125
本文介绍了如何为Hibernate JPA创建一个persistence.xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Hibernate JPA,但我需要创建我的persistence.xml(所以我可以正确使用实体管理器)。我不确定要创建什么以及将它放在哪里。

I'm trying to use Hibernate JPA but I need to create my persistence.xml (so I can use the entity manager correctly). I am unsure what to create and where to place it.

这就是我在'Core'模式下配置hibernate.cfg.xml的方式。
我正在使用:Eclipse Java EE IDE Web开发者版本:Indigo Release

This is how my hibernate.cfg.xml in 'Core' mode configured. I'm using : Eclipse Java EE IDE Web Developers version:Indigo Release

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">XXXXXX</property>
        <property name="hibernate.connection.url">jdbc:mysql://<hostname>/<database></property>
        <property name="hibernate.connection.username">XXXXX</property>
        <property name="hibernate.default_schema">XXXXXX</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>


推荐答案

创建驻留在META中的persistence.xml文件-INF文件夹。

Create a persistence.xml file that resides in the META-INF folder.

示例:

Example:

<persistence 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"
         version="2.0">
<persistence-unit name="sample">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <jta-data-source>java:/DefaultDS</jta-data-source>
  <mapping-file>ormap.xml</mapping-file>
  <jar-file>MyApp.jar</jar-file>
  <class>org.acme.Employee</class>
  <class>org.acme.Person</class>
  <class>org.acme.Address</class>
  <properties>
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="hibernate.connection.password">XXXXXX</property>
     <property name="hibernate.connection.url">jdbc:mysql://<hostname>/<database></property>
     <property name="hibernate.connection.username">XXXXX</property>
     <property name="hibernate.default_schema">XXXXXX</property>
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   </properties>
  </persistence-unit>
</persistence>

这篇关于如何为Hibernate JPA创建一个persistence.xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆