0

I already have a entity class with id as Integer. As requirement we need to add a GUID to this that wont be PK for now. But should get auto populated whenever a entity is created new. (Existing will have this new Column GUID as NULL). GUID is not getting populated(coming as NULL) with each new entry in this table. Please help.

@Entity

@Table(name = "TABLEA")

public class TableA

{

private int id;

private String guid;   

@Id
@GenericGenerator(name = "sequence", strategy = TableNameSequenceGenerator.TABLE_NAME_SEQUENCE_GENERATOR)
@GeneratedValue(generator = "sequence")
@Column(name = "ID")
public int getId()
{
    return id;
}

public void setId(int id)
{
    this.id = id;
}


@GenericGenerator(name = "guidGenerator", strategy = "guid")
@GeneratedValue(generator = "guidGenerator")
@Column(name = "GUID")
public String getGuid()
{
    return guid;
}

public void setGuid(String guid)
{
    this.guid = guid;
}

}

Monika
  • 21
  • 1
  • 5

1 Answers1

0

There's a class called Guid, if you create a variable ex. Guid guid = new Guid();

you can then use guid = Guid.NewGuid(), you can use this in the method you return the guid and it will generate a new Guid each time.

I guess there's a different way of doing it, but this is how I've done it.

Hope it helps.

  • Probably, this Guid.NewGuid() is in C#. I need the solution in JAVA. Working with Hibernate JPA. – Monika Sep 18 '12 at 12:08
  • ONE Correction. I had NOT Put @Id on GUID Column mapping. – Monika Sep 18 '12 at 12:15
  • I tried setting it through tableA.setGuid(GUID.generateGUID()); But getting now "nested exception is java.sql.SQLException: ORA-01465: invalid hex number". Seems generated GUID is NOT Hex Number. Any help would be appreciated. – Monika Sep 18 '12 at 12:59