我正在尝试写一个持久性测试。我有一个Maven项目,我使用Arquillian和嵌入式glassfish容器,Hibernate(作为我的JPA提供商)和内存中的HSQLDB作为我的存储。
当我运行测试时,我得到一个语法异常(如果需要,我可以发布完整的堆栈跟踪:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Syntax error: Encountered "identity" at line 1, column 6.
日志似乎显示了正确创建的表:
Hibernate:
create table Game (
id bigint generated by default as identity (start with 1),
title varchar(50) not null,
primary key (id)
)
Dumping old records...
Hibernate:
delete
from
Game
Inserting records...
Hibernate:
insert
into
Game
(id, title)
values
(default, ?)
Hibernate:
drop table Game if exists
Hibernate:
drop table Member if exists
我已经包括了我认为相关的文件
坚持不懈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="testPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Member</class>
<class>Game</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:demodb" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
pom.xml
<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>....</groupId>
<artifactId>....</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Web App</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
<arquillian.version>1.0.0.Alpha2</arquillian.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.3.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>arquillian-glassfish-embedded</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>....</finalName>
</build>
<repositories>
<repository>
<url>http://download.java.net/maven/2/</url>
<id>hibernate-support</id>
<layout>default</layout>
<name>Repository for library Library[hibernate-support]</name>
</repository>
<repository>
<url>http://ftp.ing.umu.se/mirror/eclipse/rt/eclipselink/maven.repo</url>
<id>eclipselink</id>
<layout>default</layout>
<name>Repository for library Library[eclipselink]</name>
</repository>
</repositories>
</project>
实体
//imports removed
@Entity
public class Game implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
public Game() {}
public Game(String title) {
this.title = title;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@Size(min = 3, max = 50)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Game@" + hashCode() + "[id = " + id + "; title = " + title + "]";
}
}
*测试类
//imports removed
@RunWith(Arquillian.class)
public class GamePersistenceTest {
@Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Game.class.getPackage())
.addAsResource("META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
private static final String[] GAME_TITLES = {
"Super Mario Brothers",
"Mario Kart",
"F-Zero"
};
@PersistenceContext
EntityManager em;
@Inject
UserTransaction utx;
// tests go here
@Test
public void shouldFindAllGamesUsingJpqlQuery() throws Exception {
// given
String fetchingAllGamesInJpql = "select g from Game g order by g.id";
// when
System.out.println("Selecting (using JPQL)...");
List<Game> games = em.createQuery(fetchingAllGamesInJpql, Game.class).getResultList();
// then
System.out.println("Found " + games.size() + " games (using JPQL):");
assertContainsAllGames(games);
}
private static void assertContainsAllGames(Collection<Game> retrievedGames) {
Assert.assertEquals(GAME_TITLES.length, retrievedGames.size());
final Set<String> retrievedGameTitles = new HashSet<String>();
for (Game game : retrievedGames) {
System.out.println("* " + game);
retrievedGameTitles.add(game.getTitle());
}
Assert.assertTrue(retrievedGameTitles.containsAll(Arrays.asList(GAME_TITLES)));
}
@Before
public void preparePersistenceTest() throws Exception {
clearData();
insertData();
startTransaction();
}
private void clearData() throws Exception {
utx.begin();
em.joinTransaction();
System.out.println("Dumping old records...");
em.createQuery("delete from Game").executeUpdate();
utx.commit();
}
private void insertData() throws Exception {
utx.begin();
em.joinTransaction();
System.out.println("Inserting records...");
for (String title : GAME_TITLES) {
Game game = new Game(title);
em.persist(game);
}
utx.commit();
// clear the persistence context (first-level cache)
em.clear();
}
private void startTransaction() throws Exception {
utx.begin();
em.joinTransaction();
}
@After
public void commitTransaction() throws Exception {
utx.commit();
}
}
这是我坚持的问题。xml。我相信我在db URL上缺少了一个属性来创建dbcreate=true
。我也放弃了JPA配置,支持Hibernate属性。虽然我还没有测试过,但我希望只需要第一次修改。我的工作文件在下面,以防有人需要。
<?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="testPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Member</class>
<class>Game</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:demodb;create=true"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
</properties>
</persistence-unit>
</persistence>
问题内容: HSQL和Hibernate的新手知识… 其次是… 打印25000(活动中的“活动”对象的数量)。但是,当我再次运行此测试时,count(*)中的对象数量并没有增加(在程序开始时为0)。因此,不会持久地写入对象。 这是我的hsqldb连接字符串: 据我所知它不是内存数据库… 有谁知道为什么对象无法在单个JVM会话之后无法持久化?乐于提供更多信息,但是与Hibernate / JPA /
我使用JPA和Hibernate作为容器管理事务的提供者(JBossAS 6.1.0.Final)。 我正在尝试实现一些细粒度的异常处理,因为我的应用程序上有一个特定的异常层次结构,所以我可以定义在任何情况下要做什么。因此,我已经研究了几个小时,发现文档很模糊,示例也有些原始,因为“为了清晰起见”总是忽略异常处理,或者是一个简单的try-catch块,用于处理异常e。 例如,采用以下代码: cat
问题内容: 在Hibernate中应如何实现模型类的equals和hashcode?有哪些常见的陷阱?默认实现在大多数情况下是否足够好?使用商务钥匙有什么意义吗? 在我看来,要考虑到延迟获取,ID生成,代理等,在每种情况下都无法正确工作。 问题答案: Hibernate有何时/如何重写一个很好的和长期的描述/ 在文档 要点是,如果您的实体将成为的一部分,或者您要分离/附加其实例,则只需担心它。后者
nexr@Entity公共类产品{ 错误:
问题内容: 对于映射到HSQLDB中的表的实体中的id字段,我具有以下定义。 但这似乎不会生成唯一的ID。而是尝试将null插入列中,这将导致失败。如果我手动创建一个序列和生成策略以使用该序列,那么数据将按预期持久保存。 自动生成策略不是暗示提供者(在这种情况下为hibernate)将自动选择正确的方法并根据需要进行所有繁重的工作(创建序列,使用本机方法或适用于该特定平台的任何方法)吗?我的理解不
问题内容: 我正在尝试使Play 2.2项目与Hibernate JPA和PostgreSQL数据库一起使用。我之前是在Play 2.1.1上做到的,它在这里非常理想。我现在收到以下错误: 我不知道这是哪里来的。我的build.sbt看起来像这样: 我的persistence.xml像这样: 我尚未编写任何代码,只是对其进行了配置。 问题答案: 好吧,我自己弄清楚了。两个版本的xml-api确实存