我不想在我的 java 源代码中进行配置,可以这样做:
@org.hibernate.annotations.Entity(
dynamicUpdate = true)
Run Code Online (Sandbox Code Playgroud)
因为我希望它是可配置的。
我不使用 hibernate.properties,只使用 persistence.xml,因为我使用的是 JPA。我怎样才能使dynamicUpdate
作为true
使用的所有实体persistence.xml
只?
我在Java Web应用程序(使用Oracle 11g数据库)中使用Hibernate 4.1.9,并且即使我正在使用c3p0池,它似乎也会获得一些失控的连接.
它似乎应该在我的配置文件中使用适当的属性进行管理,但我正在努力让它们正确设置.
这是我的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="RPRM_PERSISTENCE_UNIT" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="********"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@xxxxx.xxxx.com:1771:xxxxxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" /> <!-- hibernate 4.1.9 -->
<property name="hibernate.c3p0.acquireIncrement" value="3"/>
<property name="hibernate.c3p0.maxIdleTime" value="3600"/>
<property name="hibernate.c3p0.minPoolSize" value="6"/>
<property name="hibernate.c3p0.maxPoolSize" value="20"/>
<property name="hibernate.c3p0.maxStatements" value="20"/>
<property name="hibernate.c3p0.idleConnectionTestPeriod" value="1800"/> <!-- seconds -->
<property name="hibernate.c3p0.maxConnectionAge" value="100"/>
<property name="hibernate.c3p0.maxIdleTimeExcessConnections" value="300"/>
<property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>
<property name="hibernate.c3p0.preferredTestQuery" value="select 1 from dual"/>
<property …
Run Code Online (Sandbox Code Playgroud) 我有一个独立的JPA(Hibernate) - MySQL应用程序.
我的persistence.xml看起来像:
<persistence-unit name="JPAProj" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>
org.hibernate.ejb.HibernatePersistence
</provider>
....
<properties>
<property name='hibernate.show_sql' value='true'/>
<property name='hibernate.format_sql' value='true'/>
<property name='hibernate.dialect' value='org.hibernate.dialect.MySQL5InnoDBDialect'/>
<property name='hibernate.hbm2ddl.auto' value='update'/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/classicmodels"/>
<property name="javax.persistence.jdbc.user" value="someuser"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
我想在此应用程序中使用连接池(比如Apache DBCP),我需要对persistence.xml进行哪些更改?
我无法修复persistence.xml file not found
eclipse问题,这是一个简单的测试项目(Maven Nature),用于非常基本的EJB测试,该文件确实在src/main/resources/META-INF/...这是pom.xml的内容.尝试将文件夹添加到项目的构建路径,更新maven项目.到目前为止没有运气,任何帮助将不胜感激,谢谢!
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>LibEE</groupId>
<artifactId>LibEE</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>main.resources.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.ow2.spec.ee</groupId>
<artifactId>ow2-ejb-3.0-spec</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
这是persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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="LibEE" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/LibEE</jta-data-source> <!-- Refers to data …
Run Code Online (Sandbox Code Playgroud) 我找到了以下手册https://code.google.com/p/javamelody/wiki/UserGuideAdvanced#JPA_monitoring
它包含一些有关我应该进行的更改的信息persistence.xml
,以便让JavaMelody收集JPA/SQL统计信息.
但是,我不太清楚,是否应该provider
用JavaMelody
-provider 替换现有的,还是应该将它放到一个单独的持久性单元中?
我尝试了前一个选项,但它打破了应用程序(构建失败,出现错误,例如'无法将某些代理强制转换为EntityManagerFactory').
该参数unreturnedConnectionTimeout
在给定的一段时间后超时未返回的连接.我正在尝试决定是否应该在制作中使用它persistence.xml
?使用它的一大优点是连接池将能够从泄漏的连接中恢复.一个很大的缺点是泄漏连接将很难被发现.
我应该unreturnedConnectionTimeout
在生产应用中使用吗?如果是的话,它的价值应该是什么?我还应该考虑其他任何利弊吗?
在Wildfly(9.0.2)应用程序中是否可以有两个持久性单元?
我得到"WFLYJPA0061:持久性的unitName未指定,并有应用程序部署部署2所持久性单元定义'碧玉的web.war’要么改变应用程序部署到只有一个持久性单元定义或指定的unitName每个参照.持久性单位."
我unitName
在@PeristenceContext
注释中指定了.我读了一些我可以禁用的地方
<!--
<subsystem xmlns="urn:jboss:domain:jpa:1.1">
<jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
</subsystem>
-->
Run Code Online (Sandbox Code Playgroud)
在standalone.xml
.这删除了错误消息,但也禁用了entityManagers的注入(在代码中引用它们的空指针)
我试图将持久性单元分成两个不同的ejb-jar,每个都有自己的persistence.xml,但由于它们仍然包含在同一个战争中,Wildfly仍然抱怨.
这两个持久性单元与hibernate一起使用,一个带有postgresql数据库,另一个带有用于ms访问的ucanaccess驱动程序.他们都分开工作.
我在JPA测试类中遇到字符编码问题。
\n\n在我的h2 内存数据库中,我有这个插入查询:
\n\nINSERT INTO MYTABLE (ID,FIELD1,FIELD2) VALUES (100,\'ABC\',\'R\xc3\xa9clamation\');\n
Run Code Online (Sandbox Code Playgroud)\n\n(请注意“R\xc3\xa9clamation”中的“\xc3\xa9”字符)
\n\n在我的 JUnit 测试中,我尝试断言 FIELD2 列的值等于“R\xc3\xa9clamation”(如您所见)
\n\n但它失败并出现以下错误:
\n\n\n\n\norg.junit.ComparisonFailure:预期:R[\xc3\xa9]clamation,但\n是:R[\xef\xbf\xbd]clamation
\n
我想知道是否有一种方法可以在 persistence.xml 文件中指定字符编码(也许?或其他地方)
\n\n这是我的 persistence.xml 测试文件:
\n\n<?xml version="1.0" encoding="UTF-8"?>\n<persistence\n version="2.1"\n xmlns="http://xmlns.jcp.org/xml/ns/persistence"\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence\n http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">\n\n <persistence-unit name="myTestPU">\n <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>\n <class>com.myproject.myclass1</class>\n <class>com.myproject.myclass2</class>\n <properties>\n <property\n name="javax.persistence.schema-generation.database.action"\n value="none" />\n <property\n name="javax.persistence.sharedCache.mode"\n value="ENABLE_SELECTIVE" />\n <property\n name="javax.persistence.jdbc.url"\n value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM \'classpath:ddl/schema.sql\'\\;RUNSCRIPT FROM \'classpath:ddl/data.sql\'" />\n <property\n name="javax.persistence.jdbc.driver"\n value="org.h2.Driver" />\n <property\n name="hibernate.dialect"\n value="org.hibernate.dialect.H2Dialect" />\n <property\n name="hibernate.show_sql"\n …
Run Code Online (Sandbox Code Playgroud) 我正在尝试了解以下内容:
当我创建EJB项目并将其部署到Glassfish时,我是否在Glassfish的管理员中心设置JDBC资源/连接池,还是在persistence.xml中添加了用户名,密码等所有不同的属性?我一点都不明白。
我不明白为什么我们既有JDBC资源又有JDBC连接池。这是什么,它们之间有什么区别?有人可以向我解释这些事情还是/并且提供有关persistence.xml文件及其周围部分的良好链接?
我正在尝试将一个简单的EJB项目部署到Jboss 7.1.1上.我有一个单独的H2数据库安装.
所以我更改了standalone.xml,如下所示:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:tcp://localhost/~/test</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
Run Code Online (Sandbox Code Playgroud)
现在我还编辑了persistence.xml以匹配standalone.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="scube" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.sample.model.Property</class>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
Eclipse,在行中指出错误:java:jboss/datasources/ExampleDS
错误如下:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'jta-data-source'. One of '{"http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/
xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":shared-cache-mode, …
Run Code Online (Sandbox Code Playgroud)