我正在使用以下字段注释:
@Id<br/>
@TableGenerator( name = "comment_sequence", pkColumnValue = "comment_sequence" )<br/>
@GeneratedValue( strategy = GenerationType.TABLE, generator = "comment_sequence" )<br/>
private Long id_comment;
建表的sql是:
CREATE TABLE hibernate_sequences ( sequence_name VARCHAR(255) NOT NULL, next_val bigint, PRIMARY KEY ( sequence_name ) );<br/>
INSERT INTO hibernate_sequences VALUES ( 'comment_sequence', 1 );
但是这个实体根本就没有持久化。知道会发生什么吗?我在上面提供的代码中做错了什么吗?
编辑:
我压抑了原帖中的一些信息,对不起(半夜都快睡着了问=/)。
实体正在正确创建,如果我将策略更改为 SEQUENCE
并添加 SQL CREATE SEQUENCE hibernate_sequence
一切正常(它是持久的),但我想使用 TABLE
在 hibernate_sequences
上连续保存每个表序列的策略.
我唯一的异常(exception)是 TransactionRolledbackException
由于 NullPointerException
由集成测试中的失败测试引起。没有明确说明为什么不插入数据。
我在使用 hibernate.show_sql = true
时得到以下 hibernate 输出:
...
12:38:48,753 INFO [stdout] (pool-5-thread-1) Hibernate:
12:38:48,754 INFO [stdout] (pool-5-thread-1) insert
12:38:48,755 INFO [stdout] (pool-5-thread-1) into
12:38:48,756 INFO [stdout] (pool-5-thread-1) cm_comment
12:38:48,757 INFO [stdout] (pool-5-thread-1) (cd_status, ds_message, dt_alt, dt_inc, id_user_alt, id_user_inc, id_problem, id_comment)
12:38:48,758 INFO [stdout] (pool-5-thread-1) values
12:38:48,759 INFO [stdout] (pool-5-thread-1) (?, ?, ?, ?, ?, ?, ?, ?)
12:38:48,766 INFO [stdout] (pool-5-thread-1) Hibernate:
12:38:48,766 INFO [stdout] (pool-5-thread-1) select
12:38:48,767 INFO [stdout] (pool-5-thread-1) commentent0_.id_comment as id1_6_,
12:38:48,768 INFO [stdout] (pool-5-thread-1) commentent0_.cd_status as cd2_6_,
12:38:48,770 INFO [stdout] (pool-5-thread-1) commentent0_.ds_message as ds3_6_,
12:38:48,771 INFO [stdout] (pool-5-thread-1) commentent0_.dt_alt as dt4_6_,
12:38:48,772 INFO [stdout] (pool-5-thread-1) commentent0_.dt_inc as dt5_6_,
12:38:48,773 INFO [stdout] (pool-5-thread-1) commentent0_.id_user_alt as id6_6_,
12:38:48,774 INFO [stdout] (pool-5-thread-1) commentent0_.id_user_inc as id7_6_,
12:38:48,775 INFO [stdout] (pool-5-thread-1) commentent0_.id_problem as id8_6_
12:38:48,776 INFO [stdout] (pool-5-thread-1) from
12:38:48,777 INFO [stdout] (pool-5-thread-1) cm_comment commentent0_
12:38:48,778 INFO [stdout] (pool-5-thread-1) where
12:38:48,779 INFO [stdout] (pool-5-thread-1) commentent0_.id_problem=?
12:38:48,840 ERROR [org.jboss.arquillian.protocol.jmx.JMXTestRunner] (pool-5-thread-1)
...
java.lang.AssertionError: expected:<1> but was:<0>
...
我不确定这是否相关,但在之前的测试中我得到了错误:
12:50:36,510 INFO [org.jboss.as.ejb3] (pool-4-thread-1) JBAS014101: Failed to find SFSB instance with session ID {[-98, -17, -32, -33, 63, 107, 74, 59, -76, -127, -19, 29, 24, 45, -50, 5]} in cache
当我改变 postgresql.conf
对于 log_statement = 'all'
并检查 pg_log
运行应用程序后的目录我没有看到任何日志。所以我不确定如何启用该选项。
我还使用 Arquillian、arquillian 持久性 API 和 JBoss 托管实例进行集成测试。我将更新标签,因为这可能与其中任何一个相关。
最佳答案
TableGenerator
的“声明”没有给出任何关于它应该查看/使用哪个表的信息,你给它的只是一个 pkColumn 名称....
尝试:
@TableGenerator(
name = "comment_sequence",
表=“hibernate_sequences”,
pkColumnName = "序列名称",
valueColumnName = "next_val",
pkColumnValue = "评论序列",
分配大小=1)
allocationSize=1
应该可能大于 1 ... 当您验证它正在工作时。
(如果您使用此策略并将大量生成器放入数据库中的同一个表中,请注意潜在的锁定问题 - 如果您的应用程序正在创建“大量”实体。)
关于java - 使用 jpa 时实体不会被持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15182805/