JPA Hibernate 项目中需要 DataSource 吗?
我正在准备一些使用 JPA 2.0、Hibernate 作为提供程序、MySQL 5 作为数据库的应用程序,它将部署在 JBoss AS 7.0.2 上。
我已经在 persistence.xml 中配置了一些基础知识,并且进入了某种状态的麻烦。我注意到有些人还在 JBoss 管理控制台级别定义了一些特定的数据源。
我的问题是。我真的需要担心 Hibernate 应用程序中的某些 DataSource 或类似的东西吗?
我认为这在旧的 JDBC 方法中很重要。
在一些显示示例的书籍中,持久性中没有这样的配置。 xml 或 hibernate.cfg.xml
我是否必须将 mysql 连接器放入 JBOSS_HOME/standalone/deployments 目录才能在我的应用程序中使用 MySQL?
以下是我的 persistence.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="SomeApp">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/somedb" />
<property name="hibernate.connection.username" value="" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence>
I am preparing some application with usage of JPA 2.0, Hibernate as provider, MySQL 5 as database, which will be deployed on JBoss AS 7.0.2.
I have already configured some basics in persistence.xml and I came into some kind of trouble. I have noticed that some people also defines some specific DataSource on JBoss Management Console level.
My question is. Do I really need to worry about some DataSource or anything like that in Hibernate application?
I thought it is important in old JDBC approach.
In some books, where examples are shown, there is no such configuration in persistence.xml or hibernate.cfg.xml
Do I have to place mysql connector into JBOSS_HOME/standalone/deployments directory to use MySQL in my application?
Here is content of my persistence.xml file:
<?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="SomeApp">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/somedb" />
<property name="hibernate.connection.username" value="" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,您可以通过以下方式访问数据库:
persistence.xml
中的信息。 * 的 - 这基本上看起来像您发布的示例,在 ApplicationServer 中创建数据源并在
persistence.xml
中引用它(通过它的您在创建过程中提供的 JNDI 名称)可能与此类似(为了简洁起见,没有 XML 模式定义):您现在实际执行的操作(使用这些属性)是使用 JDBC。
我肯定会选择在 ApplicationServer 中创建数据源,而不是在
persistence.xml
的属性中提供它。它允许您动态更改最终数据库、其类型、凭据、管理连接池等,甚至无需触及描述符。它也更安全,因为凭据不会写入服务器上留下的纯文件中。
附带说明一下,请记住,
javax.persistence.jdbc.*
属性是 Java SE 环境的 JPA 提供程序必须要求,但对于Java EE来说它是可选。希望有帮助!
Well, you can either access the database by:
persistence.xml
using your jpa-provider properties (in your casehibernate.connection.*
) or the JPA 2.0 standardisedjavax.persistence.jdbc.*
ones - this basically looks like the example you've posted,creating a Data Source in the ApplicationServer and just referring to it in the
persistence.xml
(through it's JNDI name you provide during creation) which might look similar to this (without the XML schema definition for the sake of brevity) :What you're actually doing right now (with these properties) is using the JDBC.
I would definitely go with the creation of the Data Source in the ApplicationServer rather than providing it in the properties in
persistence.xml
. It allows you to dynamically change the end-database, it's type, credentials, manage connection pools, etc. without even touching your descriptor.It's also safer, as the credentials are not written in the plain file left on your server.
As a side note, please remember that the
javax.persistence.jdbc.*
properties are a JPA provider must requirement for the Java SE environment, but it's optional for Java EE.Hope that helps!
是的,您需要放置 Mysql J/connector 才能将其用作 JDBC 驱动程序。您的应用程序服务器(JBOss、Weblogic、Glassfish 等)不提供它,因为取决于您正在使用的 RDBMS(在本例中为 Mysql)及其版本。
对于 JBoss 7,可以通过以下两种方式之一将 JDBC 驱动程序安装到容器中:作为部署或作为核心模块。对于两种模式的优缺点和详细说明,您可以查看以下文档: http://community.jboss .org/wiki/DataSourceConfigurationInAS7
Yes you need to put Mysql J/connector for use it as JDBC Driver. Your application server (JBOss, Weblogic, Glassfish, etc) doesn't provide it because depend of the RDBMS that you are using (in this case Mysql) and the version of it.
In the case of JBoss 7 the JDBC driver can be installed into the container in one of two ways: either as a deployment or as a core module. For the pros/cons of both modes an detailed explanatio you can check the following documentation: http://community.jboss.org/wiki/DataSourceConfigurationInAS7