如何坚持有序
我有两个实体:EXAM 和 EXAM_NORMAL。
EXAM
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String codeName;
@Enumerated(EnumType.STRING)
private ExamType examType;
@ManyToOne
private Category category;
@OneToMany(mappedBy="id",cascade=CascadeType.PERSIST)
@OrderBy("id")
private List<Exam_Normal> exam_Normal;
EXAM_NORMAL
@Id
private Long item;
@Id
@ManyToOne
private Exam id;
@Enumerated(EnumType.STRING)
private Gender gender;
private Integer age_month_from;
private Integer age_month_to;
问题是,如果我在 EXAM 类中放置 EXAM_NORMAL 列表,如果我尝试持久化(EXAM),我会收到错误,因为它首先尝试持久化 EXAM_NORMAL 但它不能,因为 EXAM 的主键丢失了,因为它不是不坚持...
有什么方法可以定义顺序吗?或者我应该设置 null
列表,保留然后再次设置列表?
谢谢:)
I have two entities: EXAM and EXAM_NORMAL.
EXAM
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String codeName;
@Enumerated(EnumType.STRING)
private ExamType examType;
@ManyToOne
private Category category;
@OneToMany(mappedBy="id",cascade=CascadeType.PERSIST)
@OrderBy("id")
private List<Exam_Normal> exam_Normal;
EXAM_NORMAL
@Id
private Long item;
@Id
@ManyToOne
private Exam id;
@Enumerated(EnumType.STRING)
private Gender gender;
private Integer age_month_from;
private Integer age_month_to;
The problem is that if I put a list of EXAM_NORMAL at an EXAM class if I try to persist(EXAM) I get an error because it tries to persist EXAM_NORMAL first but it cant because the primary key of EXAM is missing because it isn't persisted...
Is there any way to define the order? Or should I set null
the list, persist and then set the list again?
thanks:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的注释有问题(这就是 JPA 提供程序未按正确顺序执行查询的原因)。
为什么“Exam_Normal”中有两个
@Id
(顺便说一句,这不是 Java 中命名类的标准方法)?另外,为什么你没有任何@GenerateValue
?也许先解决这个问题。如果这不起作用,请稍微改进您的问题,显示更多代码(以及创建要持久存在的实体的部分),改进格式。这会增加您获得答案的机会。在目前的状态下,复制需要花费太多的时间/精力。
There is something wrong with your annotations (which is why the JPA provider is not executing queries in the right order).
Why do you have two
@Id
in "Exam_Normal" (which is BTW not a very standard way to name a class in Java)? Also, why don't you have any@GeneratedValue
? Maybe fix this first.If this doesn't work, improve your question a bit, show more code (and also the part where you create the entities to persist), improve the formatting. This would increase your chances to get answers. In it's current state, it'd take too much time/effort to reproduce.