2013-10-23 120 views
0

大家好我是新的休眠。而我是衝浪的休眠相關主題的堆棧溢出,我發現這個關於使用休眠映射對象到數據庫

Need clarification about mapping objects to database, annotations, and one to many relationships

閱讀解決方案,我有點迷茫後開始建設的具體情況,瞭解多對一背後的實際機制,我在哪裏使用Oracle 11g數據庫。當我運行我的項目Hibernate是自動生成的表和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]) 

我做了什麼是上述專題我的解決辦法剛纔提到,我張貼我的實體類(S)請幫我解決這個

類工作

@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)的數據(我使用相同的序列爲三類,以節省時間),然後

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

我認爲發生異常是因爲數據在插入其他表之前插入到作業表中。請幫我找出我錯過了什麼?

回答

0

最後,我已經想出了與作業類相關的問題,剩下的代碼(類)都可以。我在這裏粘貼正確的和修改工作實體

@Entity 
@Table(name="JOB") 
public class Job implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @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; 


    /* The problem was inside this block */ 


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

    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL) 
    @JoinColumn (name="JOB_JOBFILTERID", nullable = false) 
    private JobFilteration jobfilteration; 
-1

看到您要存儲的對象。看起來你嘗試保存的對象對於那些不接受空值的字段爲空值。根據你的代碼,所有的數據列不能爲空。這意味着你不能對這些列中輸入空值

+0

我每天值有填補,現在我已經提交了所有的類,請看看 – NaN

1

首先嚐試保存相關實體(在你的情況下授權),那麼儘量節省外實體(工作)

session.save(Authorization's object); 
session.save(Job's object) 
+0

對於我在授權實體使用權多對一? – NaN

+0

我得到了這一點的異常的原因,我已經在這篇文章中提到,但我的問題是爲什麼即使當我們使用manytoone關係船忘記三個表讓我們只談兩個表或兩個實體類我們做同樣的事情吧?那麼在我的代碼中有什麼問題? – NaN

+0

jobdao.addPersonandCard(job);在嘗試保存auth和jfilter對象並保存作業之前,嘗試將cacade全部添加到一個批註中@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL) – Ramesh

1

既然你有內部的兩個實體主要的,你需要先保存然後設置包含它們的主類中的實體。恐怕我真的不記得哪一個函數sesssion.save或session.saveOrUpdate返回bean,但是使用該方法,使用BeanUTils.copy方法並在主類中設置bean,一旦對所有實體都是這樣,saveOrUpdate主要的實體類。

希望這會有所幫助!

+0

沒有我的觀點是要麼我上面提到的線程是不正確的,或者我做錯了我只是想知道我失蹤這裏? – NaN

+0

嗯,我猜沒有任何錯誤的註釋,但如果你有實體設置爲非空,那麼當你試圖只保存主要實體的錯誤將被拋出,因爲休眠會嘗試檢查是否相應的條目(與FK相關聯是否存在),並且由於它是全部定義的或者可能首先保存的,所以cz的空白位置是空的,所以然後我假設錯誤。 –