I need to use uniqueidentifier as Primary key for each table instead of ID (integer). The logic described below works for ID(Integer) whereas it fails for rowguid(uniqueidentifier).
I have the following tables in (MS SQL Server) DB.
CREATE TABLE Person(
[ROWGUID] [uniqueidentifier] ROWGUIDCOL NOT NULL CONSTRAINT [DF_PERSON_ROWGUID] DEFAULT (newsequentialid()),
[NICKNAME] [varchar](15) NULL,
[EMAIL] [varchar](50) NULL,
[DATE_OF_BIRTH] [date] NULL)
CREATE TABLE School(
[ROWGUID] [uniqueidentifier] ROWGUIDCOL NOT NULL CONSTRAINT [DF_PERSON_ROWGUID] DEFAULT (newsequentialid()),
[PERSONID] [varchar](15) NULL,
[EMAIL] [varchar](50) NULL,
[DATE_OF_BIRTH] [date] NULL)
It is a one-to-many relationship (Person may have multiple schools) . PERSONID of School table is ROWGUID of Person table.
On the JPA side, the problem is that, I am not able to insert a Person object with school . However, I'm able to
- insert a Person object alone in to table,
- make a select statement for a person and retrieve its schools. So I believe it proves that the FK relation works fine.
So back to the problem where I cannot make an insert; I have the following exception when trying to persist ;
Cannot insert the value NULL into column 'PERSONID', table 'SCHOOL'; column does not allow nulls. INSERT fails.
So basically, it cannot create a Person object, generate its ROWGUID, and then wire it to the column PERSONID of the table School. This is what I presume. And I believe it is because of the way I generate ID on JPA side. Here how it is;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROWGUID", columnDefinition = "uniqueidentifier")
private String rowguid;
...
public class School implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROWGUID", columnDefinition="uniqueidentifier")
private String rowguid;
//bi-directional many-to-one association to Person
@ManyToOne
@JoinColumn(name="PROFILEID")
private Person person;
public School() {
}
...
Any idea how to overcome it ?
IMPORTANT : The same logic works fine when the Primary key for each entity is ID(int) as following ;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int id;
public class School implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int id;
//bi-directional many-to-one association to Person
@ManyToOne
@JoinColumn(name="PROFILEID")
private Person person;
public School() {
}
Basically there is something wrong when I convert the following generation of ID ;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int id;
INTO
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROWGUID", columnDefinition="uniqueidentifier")
private String rowguid;