Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate oneToOne映射未读取joinColumn类实例_Java_Hibernate - Fatal编程技术网

Java Hibernate oneToOne映射未读取joinColumn类实例

Java Hibernate oneToOne映射未读取joinColumn类实例,java,hibernate,Java,Hibernate,SQL表的输出,即使代码中存在异常 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.Jo

SQL表的输出,即使代码中存在异常

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();
    }
}
Eclipse异常

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

尝试在关联的两侧设置相关对象。 保存实例时:

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)

hos.setCountry(countObj);

您可以将一个方便的方法添加到任何一个实例中,例如,因此您可以在两个实例中同时调用一个方法。

格式设置非常倾斜。我不熟悉stackoverlow,对如何设置格式感到非常困惑。问题在于,在上面的代码中,我能够准确地存储HeadOfState的值,但在同一代码中,当我尝试获取值时,我得到一个异常,即对象不存在。基本上,我检索country对象,它有HeadOfState对象hos,出于某种原因,它显示为null。即使DB表有recordTry
会话。persist
而不是
会话。save
请注意,在DB中,在hos表中,您有列host_id,在映射类中,您有以下内容:@column(name=“hos_id”)谢谢,但问题出在其他地方(代码与我发布的内容相比没有太大变化)。在具有发生异常的main方法的类中,当我使用main运行该类时,如果在第一次运行时,我执行session.save,并且正如您看到的,当我进一步检索时,我得到了我正在谈论的异常。但是在下一次运行中,如果我只是尝试检索保存的对象,它将非常有效。我想问题是别的
countObj.setHos(hos);