使用jpa时实体不会被持久化

时间:2013-03-03 05:37:24

标签: java postgresql java-ee jpa jboss-arquillian

我正在使用以下字段注释:

@Id
@TableGenerator( name = "comment_sequence", pkColumnValue = "comment_sequence" )
@GeneratedValue( strategy = GenerationType.TABLE, generator = "comment_sequence" )
private Long id_comment;

用于创建表的sql是:

CREATE TABLE hibernate_sequences ( sequence_name VARCHAR(255) NOT NULL, next_val bigint, PRIMARY KEY ( sequence_name ) );
INSERT INTO hibernate_sequences VALUES ( 'comment_sequence', 1 );

但实体根本不坚持。知道会发生什么事吗?我是否对上面提到的代码做错了什么?


编辑:

我在原帖中压制了一些信息,对不起(半夜几乎要求睡觉= /)。

正在正确创建实体,如果我将策略更改为SEQUENCE并添加SQL CREATE SEQUENCE hibernate_sequence一切正常(它是持久的),但我想使用{{1} }策略将每个表序列保存在TABLE上。

我唯一的例外是hibernate_sequences由于TransactionRolledbackException而导致集成测试中的测试失败。没有明确说明为什么它没有插入数据。

使用NullPointerException时,我得到以下hibernate输出:

hibernate.show_sql = true

我不确定这是否可以相关,但在之前的测试中我得到了错误:

... 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'目录时,我看不到任何日志。所以我不确定如何启用该选项。

我还使用Arquillian,arquillian持久性API和JBoss托管实例进行集成测试。我要更新标签,因为这可能与其中任何标签有关。

1 个答案:

答案 0 :(得分:2)

TableGenerator的“声明”没有给出任何关于它应该在/使用哪个表的信息,你给它的只是一个pkColumn名称......

尝试:

@TableGenerator( name = "comment_sequence", table = "hibernate_sequences", pkColumnName = "sequence_name", valueColumnName = "next_val", pkColumnValue = "comment_sequence", allocationSize=1)

allocationSize=1应该是大于1的东西...当你确认它正在工作时。 (如果您使用此策略并将大量生成器放入数据库中的同一个表中,请注意潜在的锁定问题 - 如果您的应用程序正在创建“大量”实体。)