0

こんにちは皆さん、私は冬眠するのが初めてです。休止状態関連のトピックのスタックオーバーフローをサーフィンしているときに、これを見つけました

オブジェクトのデータベースへのマッピング、注釈、および 1 対多の関係について明確にする必要があります。

ソリューションを読んだ後、私は少し混乱し、Oracle 11g データベースを使用した多対 1 の背後にある実際のメカニズムを理解するための正確なシナリオの構築を開始しました。プロジェクトの休止状態を実行しているとき、テーブルと FK リレーションが自動的に生成されますが、データを挿入するときにこの問題に直面しています (これは明らかです)。

WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([com.entity.Authorization#0])
Dependent entities: ([[com.entity.Job#1]])

WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    Unsaved transient entity: ([com.entity.JobFilteration#0])
    Dependent entities: ([[com.entity.Job#1]])
    Non-nullable association(s): ([com.entity.Job.jobfilteration])

私が言及した上記のトピックの解決策を正確に実行しました。エンティティクラスを投稿しています。これを解決するのを手伝ってください

クラスジョブ

@Entity
@Table(name="JOB")
public class Job implements Serializable {
    @Id 
    @SequenceGenerator(name="person_seq", sequenceName="SEQ_PERSON",initialValue=1,allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="person_seq")
    @Column(name="JOB_SERIAL")
    private int id;

    @Column(name="JOB_CATAGORY",nullable=false,length=200,unique=true)
    private String catagory;


    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn (name="JOB_AUTHID", nullable = false, updatable = false, insertable = false)
    private Authorization authorization;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn (name="JOB_JOBFILTERID", nullable = false, updatable = false, insertable = false)
    private JobFilteration jobfilteration;

クラス JobFilteration

@Entity
@Table(name="JOB_FILTERATION")
public class JobFilteration implements Serializable {
    @Id 
    @SequenceGenerator(name="person_seq", sequenceName="SEQ_PERSON",initialValue=1,allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="person_seq")
    @Column(name="FILTER_SERIAL")
    private int id;

    @Column(name="FILTERNAME",nullable=false,length=200)
    private String filtername;

クラス認可

@Entity
@Table(name="AUTHORIZATION")
public class Authorization implements Serializable {
    @Id 
    @SequenceGenerator(name="person_seq", sequenceName="SEQ_PERSON",initialValue=1,allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="person_seq")
    @Column(name="AUTH_ID")
    private int id;

    @Column(name="AUTHORISEDBY",nullable=false,length=200)
    private String authorisedby;

データを挿入するためにメソッドを作成し public void insertToDB(Job job)(時間を節約するために3つのクラスに同じシーケンスを使用しました)、次に

JoBDao jobdao= new JoBDao();
Job job= null;
Authorization auth = null;
JobFilteration jfilter = null;

try{
job= new Job();

auth = new Authorization();
    auth.setAuthorisedby("SOMEPERSON");

jfilter = new JobFilteration();
    jfilter.setFiltername("JEE");

job.setCatagory("PERMANENT");
job.setAuthorization(auth);
job.setJobfilteration(jfilter);


jobdao.addPersonandCard(job);

データが他のテーブルに挿入される前にジョブテーブルに挿入されたため、例外が発生したと思います。私が欠けているものを見つけるのを手伝ってください。

4

4 に答える 4

1

最初に依存エンティティ(あなたの場合は Authorization )を保存してから、外部エンティティ(ジョブ)を保存しようとします

session.save(Authorization's object);
session.save(Job's object)
于 2013-10-23T07:34:52.603 に答える
1

メイン エンティティ内に 2 つのエンティティがあるため、最初に保存してから、それらを含むメイン クラスにエンティティを設定する必要があります。残念ながら、関数 sesssion.save または session.saveOrUpdate のどれが Bean を返すか思い出せませんが、そのメソッドを使用し、BeanUTils.copy メソッドを使用してメイン クラス内に Bean を設定します。 、メイン エンティティ クラスを saveOrUpdate します。

お役に立てれば!

于 2013-10-23T08:52:25.060 に答える
-1

保存しようとしているオブジェクトを参照してください。保存しようとしているオブジェクトには、null 値を受け入れないフィールドの null 値があるようです。あなたのコードによると、すべてのDB列はnull可能ではありません。つまり、これらの列に null 値を入力することはできません

于 2013-10-23T05:42:21.960 に答える