您好我在Tomcat 7服务器上创建了一个JNDI资源,我正在尝试通过persistence.xml使用它,但是我收到一个错误,即在上下文中找不到资源名称.我尝试在网上找到其他解决方案,但没有为我工作.有谁看到了什么问题?在我的server.xml片段下面:
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
<Resource name="jdbc/myds" auth="Container"
type="javax.sql.DataSource"
maxActive="10" maxIdle="3" maxWait="10000"
username="boardgames" password="boardgames"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;databaseName=boardgames;integratedSecurity=true;"
/>
</GlobalNamingResources>
Run Code Online (Sandbox Code Playgroud)
我还在web.xml中添加了资源引用:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Run Code Online (Sandbox Code Playgroud)
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="persistence_sample" transaction-type="RESOURCE_LOCAL">
<!--
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
-->
<non-jta-data-source>java:/comp/env/jdbc/myds</non-jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/> <!-- Enable Hibernate's automatic …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个Java SE(桌面)应用程序,它必须访问不同的数据库,所有这些数据库都具有相同的数据模型(相同的模式,表格等).我想重用已经在每个数据库前面的Java EE应用程序中使用的JPA实体.
要重用现有的entity.jar文件,我必须使用具有resource_local数据源的不同persistence.xml重新打包它.这是构建时间的不便,但不是一个大问题.
问题是我的桌面应用程序将仅限于使用persistence.xml文件中定义的数据源.我可以定义多个持久性单元并选择在运行时使用哪个,但是当添加新数据库时,我将不得不更改persistence.xml并更新所有桌面二进制文件.
我希望能够在.properties文件中定义每个用户可以配置的新数据源.有没有办法在运行时覆盖或添加到persistence.xml中声明的持久性单元?
我不想用Web服务接口构建Java EE应用程序,只是为了支持这个桌面应用程序.Java EE应用程序有不同的用途,我希望将桌面功能保留在桌面应用程序中.
谢谢.
当我尝试本教程时,我是 JPA 和休眠的新手 。我在我的persistence.xml中添加了以下提供者,
<provider>org.hibernate.ejb.HibernatePersistence</provider>
Run Code Online (Sandbox Code Playgroud)
我收到这个错误..
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
at org.hibernate.ejb.Ejb3Configuration.addAnnotatedClass(Ejb3Configuration.java:1421)
at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1391)
at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1184)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1048)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:291)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:373)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at de.vogella.jpa.simple.main.Main.main(Main.java:17)
Run Code Online (Sandbox Code Playgroud)
请帮助解决此错误。谢谢
annotations hibernate jpa persistence.xml hibernate-annotations
我想根据所选的Maven配置文件使用两个不同的数据库.对于配置文件"生产",我想使用MySQL数据库,对于"开发"配置文件,我想使用内存中的HSQLDB.
我发现可以有两个persistence.xml文件.一个在"src/main/resources/META-INF"中,另一个存储在"src/test/resources/META-INF"中.这样就可以选择不同的数据库进行测试.
但是,是否也可以根据所选的Maven配置文件进行数据库选择?
database-connection persistence.xml java-ee maven-3 maven-profiles
我有一个使用JPA的hibernate项目.
我的persistence.xml内容如下:
<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="Demo-PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/DemoDS</non-jta-data-source>
<class>com.demo.framework.entity.ReportDefinitionEntity</class>
<properties>
<!-- Database connection -->
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.9.110:3306/demoDB" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<!-- Hibernate dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Output goodies
-->
<property name="hibernate.query.jpaql_strict_compliance" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="false" />
<!-- Cache
-->
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
现在当我使用eclipse运行它时我没有问题,但是当我在Jboss中部署它时,我得到以下错误:
错误[AbstractKernelController]安装到Start时出错:name = persistence.unit:unitName =#Demo-PU state =创建java.lang.ClassCastException:org.hibernate.ejb.HibernatePersistence无法强制转换为javax.persistence.spi.PersistenceProvider …