4

I have 4 entities one-to-many Message entity:

@Entity
@Table(name = "Messages")
public class Message {
...
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "messagesounds_id")
    private Sound sound;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "messagevibrations_id")
    private Vibration vibration;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "messagecolors_id")
    private Color color;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "messageplatforms_id")
    private Platform platform;
...
}

whereas other 4 entities look like this:

@Entity
@Table( name = "MessageSounds"  , uniqueConstraints=@UniqueConstraint(columnNames = {"name"}))
public class Sound {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @OneToMany(cascade={CascadeType.ALL})
    @JoinColumn(name="messagesounds_id")
    private Set<Message> message;
...
}

I can successfully create the first message record referenced to those first records of the 4 referenced tables. But the message table in postgresql db looks like this:

 id |...| messagesounds_id | messagevibrations_id | messagecolors_id | messageplatforms_id |
 1  |...| 2 | 3 | 4 | 5 |

I want to let every primary key of the 4 referenced tables be auto incremented, so that the first record of message table should look like this:

 id |...| messagesounds_id | messagevibrations_id | messagecolors_id | messageplatforms_id |
 1  |...| 1 | 1 | 1 | 1 |

How to achieve this using annotations?

Capitaine
  • 1,923
  • 6
  • 27
  • 46
  • Here is the answer http://stackoverflow.com/questions/2011528/hibernate-auto-increment-id. – pepuch Jun 10 '13 at 09:24
  • I have tried that in primary keys of all 4 referenced entities but it stays the same. I am thinking if it is about the hibernate sequence or limitations of postgresql – Capitaine Jun 10 '13 at 09:26
  • 2
    What about this question: http://stackoverflow.com/q/3433724/330315 or this: http://stackoverflow.com/q/684836/330315 or this: http://stackoverflow.com/a/4502062/330315 –  Jun 10 '13 at 09:42

2 Answers2

4

Ok, from Hibernate use of PostgreSQL sequence does not affect sequence table cited by a_horse_with_no_nameless,

I managed to set the auto incremented primary keys of every referenced tables like these:

@Id
@SequenceGenerator(name="pk_sequence",sequenceName="messagesounds_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="pk_sequence")
private int id;

Thanks a lot for all the research.

Community
  • 1
  • 1
Capitaine
  • 1,923
  • 6
  • 27
  • 46
-2

@Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; and you leave it null (0) when persisting. (null if you use the Integer / Long wrappers)

In some cases the AUTO strategy is resoved to SEQUENCE rathen than to IDENTITY or TABLE, so you might want to manually set it to IDENTITY or TABLE (depending on the underlying database).

It seems SEQUENCE + specifying the sequence name worked for you.

Ayaz Ali Khatri
  • 483
  • 4
  • 13
  • 1
    i have tried that and I am using postgresql. And please just don't copy from other answers without citing sources. – Capitaine Jun 10 '13 at 09:30