2014-11-24 60 views
0
package com.hibernatetest; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 
import javax.persistence.Transient; 

@Entity 
@Table(name = "country") 
public class Country { 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name = "ctry_id") 
private Integer id; 

@Column(name = "ctry_name") 
private String name; 


@Transient 
private int rank; 

@ManyToOne 
@JoinColumn(name="fk_cont_id") 
private Continent continent; 

@OneToOne(mappedBy="country") 
private HeadOfState hos; 

public HeadOfState getHos() { 
    return hos; 
} 

public void setHos(HeadOfState hos) { 
    this.hos = hos; 
} 

public Continent getContinent() { 
    return continent; 
} 

public Country() { 
    ; 
} 
public Country(String name) { 
    this.name = name; 
} 
public void setContinent(Continent continent) { 
    this.continent = continent; 
} 

public Integer getId() { 
    return id; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public void setRank(int rank) { 
    this.rank = rank; 
} 

public int getRank() { 
    return rank; 
} 

public void addCountryToDb(Continent cont) { 
    continent = cont; 
} 

} 


package com.hibernatetest; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 

@Entity 
@Table(name="hos") 
public class HeadOfState { 
@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="hos_id") 
private Integer id; 

@Column(name="hos_name") 
private String name; 

@OneToOne 
@JoinColumn(name="fk_ctry_id") 
private Country country; 

public HeadOfState() { 

} 

public HeadOfState(String name) { 
    this.name=name; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Country getCountry() { 
    return country; 
} 

public void setCountry(Country country) { 
    this.country = country; 
} 



} 

Session session = HibernateUtil.getSessionFactory().openSession(); 
    try { 
    Transaction transaction = session.getTransaction(); 
    Country countObj = (Country)session.get(Country.class, 62); 
    HeadOfState hos = new HeadOfState("ghanaKing"); 
    hos.setCountry(countObj); 
    transaction.begin(); 
    session.save(hos); 
    transaction.commit(); 

    Country cont = (Country)session.get(Country.class, 62); 
    System.out.println("name of country is "+cont.getName()); 
    System.out.println("name of continent is "+cont.getContinent().getName()); 
    HeadOfState newhos = cont.getHos(); 
    if (newhos == null) { 
     throw new Exception("hos obj is null even after storing it"); 
    } 
    System.out.println("Name of HOS is "+cont.getHos().getName()); 
    } catch (Exception ex) { 
     System.out.println("exception has happened: "+ex.getMessage()); 
     throw ex; 
    } finally { 
     session.close(); 
     HibernateUtil.shutdown(); 
    } 
} 

輸出的SQL表即使在代碼中的異常休眠oneToOne映射不讀joinColumn類實例

ysql> select * from hos; 
host_id, hos_name, fk_ctry_id 
2,  ghanaKing,62 
1 row in set (0.00 sec) 

mysql> select * from country 
ctry_id, ctry_name, fk_cont_id 
60, England, 30 
62, ghana, 30 

Eclipse的例外

name of country is ghana 
name of continent is Asia 
exception has happened hos obj is null even after storing it 

Nov 24, 2014 2:54:29 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://172.23.180.81:3306/test] 
Exception in thread "main" java.lang.Exception: hos obj is null even after storing it 
    at com.hibernatetest.SqlTest.main(SqlTest.java:37) 
+0

格式化偏斜。我對stackoverlow很陌生,並且對如何格式化非常困惑。問題是在上面的代碼中,我能夠準確地存儲HeadOfState的值,但是在我嘗試獲取值時使用相同的代碼,我得到一個異常,該對象不存在。基本上,我檢索國家對象,它有HeadOfState對象hos,出於某種原因,它出現爲null。即使數據庫表有記錄 – curiousengineer 2014-11-24 23:11:02

+0

嘗試'session.persist'而不是'session.save' – geert3 2014-11-25 09:07:28

+1

請注意,在您的數據庫中,在hos表中,您有列host_id,並且在您的映射類中有以下內容:@Column (name =「hos_id」) – drgPP 2014-11-25 09:13:24

回答

0

嘗試兩側設置相關對象協會。 當您保存您的實例:

hos.setCountry(countObj); 

,並添加

countObj.setHos(hos); 

你可以一個方便易方法添加到您的任何實例一樣的,因此,你會打電話兩種方法之一INSEAD。

+0

謝謝,但問題是其他地方(沒有更改我發佈的代碼)。在具有發生異常的主要方法的類中,當我使用main運行該類時,如果在第一次運行中,我會執行session.save,並且當您看到我進一步檢索時,我會得到我正在討論的異常。但在下一次運行中,如果我只是嘗試檢索保存的對象,它完美地工作。問題是我想的其他事情 – curiousengineer 2014-11-25 18:07:04