java - Add a foreign key to composite primary key and changes in JPA Entity class -
java - Add a foreign key to composite primary key and changes in JPA Entity class -
i have class user primary key (id) corresponds 'user' table in sql server database. user class has many 1 relationship project entity.
public class user{ @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @column(name = "id") private integer id; @joincolumn(name = "project", referencedcolumnname = "id_project") @manytoone(optional = false) private project project; }
database:
create table [dbo].[user]( [id] [int] identity(27,1) not null, [name] [varchar](45) null, [project] [int] not null, constraint [pk_user_1] primary key clustered ( [id] asc)
now in database need have 2 rows same user ids different projects changed primary key composite primary key in user table.
user table: id name id_project --------------------------- 1 john 5 1 john 6 project table: id name --------------- 5 project5 6 project6
and changed table
create table [dbo].[user]( [id] [int] identity(27,1) not null, [name] [varchar](45) null, [project] [int] not null, constraint [pk_user_1] primary key clustered ( [id] asc, [project_mode] asc)
my question that: step required in table? , if was, how can alter @id of user class?
jpa 2.1 allows derived ids, allowing mark relationships beingness part of id. user this:
@entity @idclass(userpk.class) public class user{ @id @basic(optional = false) @column(name = "id") private integer id; @id @joincolumn(name = "project", referencedcolumnname = "id_project") @manytoone(optional = false) private project project; } public class userpk{ private integer id; private integer project;//use same type project's id. }
java sql-server jpa eclipselink many-to-one
Comments
Post a Comment