0

I'm using MS SQL Server + Hibernate (JPA, more precisely, with EntityManager) and i faced with the problem: I need to store entity into appropriate table in my DB; this table has uniqueidentifier as primary key; and storing entity already has UUID (it's primary key), with witch it should be inserted into the DB.

Problem is that when i try to merge my entity, hibernate do some magic and store my entity with another UUID value. So, when I fetch this entity from db and take a look on it ID, I see inappropriate value. So, I want to tell hibernate not to do that (or, maybe, there is another solution?).

My entity class:

public class Entity extends BaseEntity {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "id_column")
    private UUID uuId;

    // other fields, getters, setters
}

Also, I have one more 'annoyance': my IDE and my DB browser shows me another ID in that column. I mean, when I stop my app in the debug mode and look at the id of an entity, I see different value from a value in the database.

Thanks in advance!

a.kozlenkov
  • 63
  • 1
  • 9
  • Not a Hibernate expert, but why are you using `merge`? Shouldn't you just use `save` instead? `save` should preserve the initial ID. – dimoniy Aug 09 '15 at 20:47
  • @dimoniy Maybe, I was incorrect with terms I used. I'm using EntityManager API to provide my operations, which have `persist` and `merge` operations to write something into DB. – a.kozlenkov Aug 09 '15 at 21:49
  • how are you doing your merge? Why aren't you using a strategy of guid? That is usually more appropriate for SQL Server. – Tea Curran Aug 10 '15 at 05:42
  • @TerrenceCurran Can you tell me more/share link about using guid strategy? – a.kozlenkov Aug 10 '15 at 08:26
  • _"So, when I fetch this entity from db and take a look on it ID, I see inappropriate value."_ How do you fetch it? By the _old_ id? – Dragan Bozanovic Aug 10 '15 at 13:33
  • @DraganBozanovic no, I fetch entity with all another entites, when I reload my page, and if I open it - I see another id - not old value and not passed to store value. – a.kozlenkov Aug 10 '15 at 14:57
  • You are sure it is not the [Eclipse debugger object id](http://stackoverflow.com/questions/12578608/how-to-get-eclipse-debug-id-of-an-object)? Are you sure that the primary key has changed? – Dragan Bozanovic Aug 10 '15 at 14:59
  • @DraganBozanovic I'm using IDEA, but I'm shure, that it is exactly entity id. And yes, it has changed. – a.kozlenkov Aug 10 '15 at 15:02

0 Answers0