1

I am using the following field annotations:

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

The sql for creating the table is:

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 );

But the entity simply is not persisting. Any idea of what could be happening? Am I doing something wrong with the code presented above?


EDIT:

I suppressed a few information in the original post, I'm sorry (asked in the middle of night almost sleeping =/).

The entity is being properly created, if I do change the strategy to SEQUENCE and add the SQL CREATE SEQUENCE hibernate_sequence everything works fine (it is persisted), but I want to use the TABLE strategy to hold each table sequence in a row on hibernate_sequences.

The only exceptions I have is the TransactionRolledbackException due to a NullPointerException caused by a failing test in the integration test. Nothing explicit for why it is not inserting the data.

I get the following hibernate output when using 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>  

...

I am not sure if this can be related but in a previous test I get the error:

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

When I change postgresql.conf for log_statement = 'all' and check in the pg_log directory after running the app I dont see any logs. So I am not sure how I can enable that option.

I am also using Arquillian, arquillian persistence API, and a JBoss managed instance for integration testing. I am going to update the tags cause this could be related to any of those.

4
  • 5
    Yes, you're doing something wrong based on the fact that it's not working. If you want more than that, you'll need to show a lot more of your code and configuration as well as any errors you might be getting. Preferably provide an SSCCE. Commented Mar 3, 2013 at 5:49
  • Did you create the entity? Did you persist it? We can't see any of that. You should also enable log_statement in PostgreSQL and then examine the PostgreSQL log file for statements and errors that may be relevant. Make sure you code doesn't swallow exceptions, too. Commented Mar 3, 2013 at 12:05
  • @RyanStewart I thought that was implicit "Am I doing something wrong with the code presented above?". @CraigRinger I will edit my post explaining a little further, I just thought that portion of code could have something wrong because if I change the strategy to SEQUENCE everything works fine. Commented Mar 3, 2013 at 15:32
  • You're still not showing any code. The test probably fails because the id_problem you're looking for is not the id_problem that has been inserted just before. And you're not seeing any exception probably because they're caught and ignored. Commented Mar 3, 2013 at 16:18

1 Answer 1

2

Your "declaration" of TableGenerator isn't given any info about which table it should look in / use, all you're giving it is a pkColumn name ....

Try:

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

allocationSize=1 should probably be something larger than 1 ... when you've verified it is working. (Down the line, be aware of potential locking problems if you use this strategy and put a lot of generators into the same table in the db - if you're application is creating "lots of" entities.)

Sign up to request clarification or add additional context in comments.

3 Comments

When I change the allocationSize to 1 it works. The thing is I am removing the sequence tables on each test then I was assuming the next sequence to look for in test was 1. Perhaps the value 50 is being hold in memory somehow? I am trying to figure out why that is happening. And about the sequences, do you recommend using one table for each entity to avoid locking?
I can't say why/how - it probably has to do with how you "configure" arquillian and write this specific test. IMHO, you shouldn't care that much, since you're deleting/re-creating the sequence table each test you're not really testing anything real about the sequence generating ID part of the entity. ... . No, in my epxerience tablegenerators are fine (only ever seen locking problems with faulty configurations). It is good to be aware of, but the write(lock)s should be extremly short as long as they are in their own db-transaction (I think how to configure that depends on JPA implementation.)
If you edit your answer specifying that the allocationSize = 1 is the solution, then I will accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.