views:

49

answers:

1

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:)

+1  A: 

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.

Pascal Thivent