简体   繁体   中英

Jpa pojo and relation mapping in hibernate

Currently I have a pojo

class Myclass{
    private int taskid;
    private String tasktype;
    private int Monitory_by;
    private int Assigned_by;
    private int Report_to;
    private int Assigned_to;
}

where Monitory_by ,Assigned_by, Report_to,Assigned_to are primary key values of users so I have pojo for users which gives all the data of users.

The point is to relate this to pojo where Monitory_by,Assigned_by fields are optional (ie, the values may be given or not). please help me

使用Integer而不是int ,它可以为空。

Based on the tags you have used I am assuming that you are using Hibernate with JPA. I suggest going over the extensive documentation Hibernate provides on entity mapping . This reference includes help on primary key mapping (Ids) and foreign key (one-to-many) mappings.

Are you sure you want to use primitive types to mark relationship with another entity?

In JPA world you typically use entity types instead of straight primary keys. So instead of int Assigned_to you'd have User assignedTo .

If so, than you could define optionality of the relationship by using optional attribute of one of relationship annotations like @OneToMany , @OneToOne , @ManyToOne or @ManyToMany , so it would be ie:

@OneToOne(optional = true)
private User assignedTo;

PS. By default all relationships are optional, so setting optional = true is not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM