如何坚持有序

发布于 2024-08-25 14:11:30 字数 888 浏览 10 评论 0原文

我有两个实体: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等往事风中吹 2024-09-01 14:11:30

您的注释有问题(这就是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文