1

StudentsInGroup と GroupRegistration の 2 つのテーブルとオブジェクトがあります。

class StudentInGroup {
@Id
@Column (name = "ID")
private Integer id;

// other fields

@ManyToOne(fetch = FetchType.EAGER, targetEntity = GroupRegistration.class)
@JoinColumn(name = "GROUP_CODE")
private GroupRegistration groups;

// other methods
}


class GroupRegistration {
@Id
@Column (name = "ID")
private Integer Id;

// other fields

@Column(name = "GROUP_CODE")
private Integer groupCode;  // this is not primary key

// other methods
}

フォームとメソッド insertStudentInGroup(StudentInGroup student); があります。しかし、このメソッドを呼び出すと例外が発生しました: オブジェクトは保存されていない一時インスタンスを参照しています - フラッシュする前に一時インスタンスを保存します: StudentInGroup.groups -> GroupRegistration

人々がこの種の問題を抱えているのを見ましたが、彼らはカスケードを使用していました。カスケードは必要ありません。誰でも私を助けることができますか?

4

2 に答える 2

2

カスケードを使用しない場合は、まずGroupRegistration (非所有側) を保存してから StudentInGroup (所有側) を保存する必要があります。そうしないと、マッピングを行う方法を認識できないため、TransientObjectException を表示します。 . それがあなたを助けることを願っています。

于 2013-10-28T12:16:27.960 に答える
0

これにより、時間を節約できます。

GroupRegistrationが既に存在する場合、StudentInGroup を保存するときに、GroupRegistrationプロパティ適切に null に設定されていることを確認するか、適切な ID (主キー) 値を保持しているかどうかを確認する必要があります。

Employee emp = new Employee();//or from UI modal.
emp.setDept((emp.getDept().getDeptId != null) ? new Dept(emp.getDept().getDeptId) : null); 
//When getting from UI, we need to explicitly set the property to null to make hibernate happy.:-)
em.persist(emp);
于 2017-03-15T21:46:07.773 に答える