编辑:不重复但差不多
我想让我的应用程序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_1_0.xsd"
version="1.0">
<persistence-unit name="appName" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="${db.dialect'}"/>
<property name="javax.persistence.jdbc.driver" value="${db.driver}"/>
<property name="javax.persistence.jdbc.user" value="${db.user}"/>
<property name="javax.persistence.jdbc.password" value="${db.password}"/>
<property name="javax.persistence.jdbc.url" value="${db.url}"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
从源文件夹中某处的简单文本文件中获取这些占位符值.
我读到了使用Spring做的可能性
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/foo/jdbc.properties</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是在这里我们不使用Spring,只使用Hibernate和一些Primefaces.
可能吗?
谢谢!
编辑:我没有提到一些东西,但作为参考,我也使用Shiro Security和Ant来做一些事情.我将发布解决方案作为答案.这使我的项目有3个不同的文件与数据库参数:
我使用eclipseLink和jpa.在我的persistence.xml中,我定义为生成一个create.sql文件.将生成该文件,但缺少';' - 每个sql语句的分隔符.
是否有可能在persistence.xml中或以其他方式定义分隔符?
示例persistence.xml:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>de.company.project.models.User</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.application-location" value="/sql" />
<property name="eclipselink.create-ddl-jdbc-file-name" value="create.sql"/>
<property name="eclipselink.drop-ddl-jdbc-file-name" value="drop.sql"/>
<property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
</properties>
Run Code Online (Sandbox Code Playgroud)
示例生成的sql文件:
CREATE TABLE app_user (
ID INTEGER NOT NULL,
last_name VARCHAR(50) NOT NULL,
username VARCHAR(15) NOT NULL,
user_password VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
PRIMARY KEY (ID))
CREATE TABLE SEQUENCE (
SEQ_NAME VARCHAR(50) NOT NULL,
SEQ_COUNT DECIMAL(38),
PRIMARY KEY (SEQ_NAME))
INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT)
values ('SEQ_GEN_TABLE', 0)
Run Code Online (Sandbox Code Playgroud) 我想让持久性提供程序(EclipseLink 2.5.0)使用持久性单元属性"javax.persistence.schema-generation.create-script-source"和一个有效的SQL-自动在已存在的数据库中创建表. DDL脚本.
persistence.xml中:
<property name="javax.persistence.schema-generation.create-script-source" value="data/ddl.sql"/>
Run Code Online (Sandbox Code Playgroud)
ddl.sql:
USE myDatabase;
CREATE TABLE MyTable (
id INTEGER NOT NULL AUTO_INCREMENT,
myColumn VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8_bin;
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
[EL Warning]: 2014-02-12 13:31:44.778--ServerSession(768298666)--Thread(Thread[main,5,main])--Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to …
Run Code Online (Sandbox Code Playgroud) 我正在使用hibernate 3.5和JPA 2.0构建桌面应用程序.
我有2个罐子,
lib,定义每个实体和DAO,包如下所示:
org.my.package.models
org.my.package.models.dao
org.my.package.models.utils
Run Code Online (Sandbox Code Playgroud)
在org.my.package.utils我定义了我的hibernate实用程序类来获取EM和EMF实例,这意味着lib绑定到持久性单元名称但是现在这不是问题(无论如何你可以推荐我一个更好的管理方法)那)
第二个jar构建如下:
org.my.package.app
META-INF是在项目的根目录上定义的,这意味着在我的jar中我可以直接在根目录中找到这个目录:
META-INF/
META-INF/persistence.xml
org/
org/my/
...
org/my/package/app/Main.class
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,hibernate无法找到persistence.xml,它会抛出异常,例如"未找到PersistenceUnitName的包或类".
SLF4J: The requested version 1.5.11 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
3 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.0-Final
25 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.0-Final
28 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
33 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
41 [main] INFO org.hibernate.cfg.Environment - …
Run Code Online (Sandbox Code Playgroud) 我正在将JPA用于java类,并且无法将持久性XML文件放在应该存在的位置.
我使用的IDE是Eclipse Helios.Eclipselink jar文件已下载并添加到我的JRE系统库和所有文件中.在收到以下错误后,我用标签写了persistence.xml:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named customers
Run Code Online (Sandbox Code Playgroud)
提供者标签:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
Run Code Online (Sandbox Code Playgroud)
该程序仍然没有运行所以我想知道我应该在哪里放置persistence.xml(即src/main/resources,或lib/META-INF ......等)
在我的应用程序中,我使用Hibernate与SQL Server数据库,所以我设置
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect">
Run Code Online (Sandbox Code Playgroud)
在我的persistence.xml中.
在某些情况下,我想用NULL包括排序记录,我使用关键字NULLS FIRST.
因为Hibernate中的CriteriaQuery/CriteriaBuilder默认不支持它,所以我使用Interceptor来修改本机查询.
问题是,SQL Server不支持关键字NULLS FIRST,因此我使用关键字:
case when column_name is null then 0 else 1 end, column_name
Run Code Online (Sandbox Code Playgroud)
如果我想将数据库从SQL Server迁移到Oracle(例如),那么我需要在我的拦截器中放置if-else,选择我正在使用哪种方言,对吧?
这就是我说明它们的方式:
String dialect = ..............
if (dialect.equals("org.hibernate.dialect.SQLServerDialect")) { // get SQL Server dialect
// put keyword "case when column_name is null then 0 else 1 end, column_name"
} else {
// put keyword "NULLS FIRST/LAST"
}
Run Code Online (Sandbox Code Playgroud)
如何在运行时获取方言配置(在persistence.xml中)?
我正在使用JBoss Developer Studio.我有一个带有persistence.xml文件的项目.该文件对我来说很完美,但我在列出项目所有问题的选项卡上不断收到此错误.
Class "[Ljava.lang.String;@22ec7158" cannot be resolved
Run Code Online (Sandbox Code Playgroud)
我将图片包含在内,以获得更好的背景.
当我点击错误时,它会将我带到发生错误的地方,它会将我带到文件的末尾.
persistence.xml中
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MemberOfferDS</jta-data-source>
<class>com.bbb.memberoffer.model.SycsCoordinator</class>
<class>com.bbb.aicweb.memberoffer.model.SycsCoordinatorPhoneNumber</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClub</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorSecurityGroup</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClubPk</class>
<class>com.bbb.memberoffer.model.PhoneNumberType</class>
<class>com.bbb.memberoffer.model.Club</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Not sure if this is the right one to use or not? -->
<property name='hibernate.show_sql' value='true' />
<property name='hibernate.format_sql' value='true' />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud) 我希望能够将我的数据库信息存储在pom.xml中(作为属性),并将必要的值注入我的persistence.xml文件中.有什么方法可以在maven中实现这个目标吗?
另一种方法是如何将我的数据库连接信息保存在一个文件中,并能够将它提供给我的pom.xml和我的persistence.xml
我正在研究eclipse上的java spring项目,当我试图通过JUnit测试运行项目时,我得到并且错误:找不到类[org.hibernate.ejb.HibernatePersistence].我找了很多类似的问题,没有人可以解决我的问题.我正确地构建和重建项目,但仍然是同样的问题.
首先是我的JUnit测试:
@Test
public void test() {
try {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
assertTrue(true);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot find class [org.hibernate.ejb.HibernatePersistence]
Run Code Online (Sandbox Code Playgroud)
这是我的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="GK_SHOP" …
Run Code Online (Sandbox Code Playgroud) 我有一个带有 glassfish 服务器并使用 EclipseLink (JPA 2.1) 的动态应用程序。我以前可以persistence.xml
直接把jdbc配置放进去,没有任何问题。但现在它迫使我为 glassfish 创建一个数据源名称并将 jdbc 配置放入glassfish-resources.xml
文件中。
问题是我想将 mysql 的字符编码设置为 utf8,因此,我使用下面的 url 作为 jdbc url:
jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull;&characterEncoding=UTF-8;
Run Code Online (Sandbox Code Playgroud)
但我得到这个例外:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: The connection property 'zeroDateTimeBehavior' only accepts values of the form: 'exception', 'round' or 'convertToNull'. The value 'convertToNull;' is not in this set.
Run Code Online (Sandbox Code Playgroud)
我想知道如何在 url 中添加带有 & 符号的多个参数?:D
另外,我不想创建 JNDI 数据源,而是手动将 jdbc …