2013-03-03 53 views
1

當我使用下面的字段註釋實體不被持久化:使用JPA

@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一切工作正常(其持續存在),但我想用TABLE策略連續召開各表順序hibernate_sequences

我唯一的例外是TransactionRolledbackException歸因於集成測試中失敗測試導致的NullPointerException。沒有明確說明爲什麼不插入數據。

我使用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.conflog_statement = 'all'和運行應用程序,我沒有看到任何日誌後入住的pg_log目錄。所以我不確定如何啓用該選項。

我也是用的Arquillian,的Arquillian持久化API和JBoss的管理實例進行集成測試。我將更新標籤,因爲這可能與任何這些標籤有關。的TableGenerator

+5

是,基於事實並非如此,您正在做錯事。如果你想比這更多,你需要證明你的代碼和配置,以及你可能會得到任何錯誤的多很多。最好提供[SSCCE](http://sscce.org/)。 – 2013-03-03 05:49:07

+0

您是否創建了實體?你堅持了嗎?我們看不到這些。你也應該讓'PostgreSQL中log_statement'然後檢查報表和錯誤,可能是有關PostgreSQL的日誌文件。確保你的代碼不會吞下異常。 – 2013-03-03 12:05:10

+0

@RyanStewart我認爲這是隱含的「我在做錯上面的代碼嗎?」。 @CraigRinger我會編輯我的文章解釋一點點,我只是認爲這部分代碼可能有錯誤,因爲如果我將策略更改爲「SEQUENCE」,一切正常。 – 2013-03-03 15:32:52

回答

2

你的「宣言」沒有給出任何信息,關於哪個表應該查看/使用,所有你給予它是一個pkColumn名字....

嘗試:

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

allocationSize=1大概應該是大於1?當你確認這是工作。 (上下行,如果你使用這種策略,並投入了大量的發電機到同一個表在DB注意潛在的鎖定問題 - 如果你的應用程序創建「大量的」實體)

+0

當我將'allocationSize'更改爲1時,它可以工作。問題是我正在刪除每個測試中的序列表,然後我假定在測試中查找的下一個序列是1.也許值50以某種方式保存在內存中?我想弄清楚爲什麼會這樣。關於序列,你是否建議爲每個實體使用一個表來避免鎖定? – 2013-03-03 20:23:03

+0

我不能說爲什麼/如何 - 它可能與你如何「配置」arquillian並編寫這個特定的測試有關。恕我直言,你不應該在乎那麼多,因爲你刪除/重新創建序列表每個測試你沒有真正測試實體的序列生成ID部分的真實情況。 ......。不,在我的epxerience tablegenerators中沒有問題(只能看到使用錯誤配置鎖定問題)。這是很好的意識,但寫(鎖)應該是極短的,只要他們在自己的數據庫事務(我認爲如何配置這取決於JPA實現)。 – esej 2013-03-03 20:44:08

+0

如果您編輯您的答案指定'allocateSize = 1'是解決方案,那麼我會接受它。 – 2013-03-03 21:06:18