英文:
How to make a @OneToMany field mandatory in JPA2?
问题
我正在为一个应用程序设计持久性存储库。
我以这段代码作为示例:https://stackoverflow.com/questions/5858113/how-to-make-a-manytoone-field-mandatory-in-jpa2,但我需要相反的情况。
在我的情况下,每个人都必须至少有一篇文章。
你可以帮助我吗?
@Entity
@Table(name="tb_people")
public class Person{
@Id
@GeneratedValue
public long id;
@OneToMany(mappedBy="person")
List<Post> listOfPosts;
//.... 一些代码
}
@Entity
@Table(name="tb_posts")
public class Post{
@Id
@GeneratedValue
public long id;
@ManyToOne
@JoinColumn(name = "person_id")
Person person;
//.... 一些代码
}
英文:
I am designing the persistence repository for an app.
I took this list of code as example https://stackoverflow.com/questions/5858113/how-to-make-a-manytoone-field-mandatory-in-jpa2 , but I need the opposite scenario.
In my case each person must have at least one Post.
Could you help me with this?
@Entity
@Table(name="tb_people")
public class Person{
@Id
@GeneratedValue
public long id;
@OneToMany(mappedBy="person")
List<Post> listOfPosts;
//.... some code
}
@Entity
@Table(name="tb_posts")
public class Post{
@Id
@GeneratedValue
public long id;
@ManyToOne
@JoinColumn(name = "person_id")
Person person;
//.... some code
}
专注分享java语言的经验与见解,让所有开发者获益!
评论