Hibernate 一对一映射示例

在这个例子中,我们将看到如何使用注解来实现一对一的关系。

以国家和首都为例。一个国家有一个首都。下面是它们之间的关系图。

Hibernate 一对一映射示例_第1张图片

现在要在数据库中创建上述表,您需要创建两个 java 文件,即 Country.java 和 Capital.java。

1.Country.java

Country 类将用于在数据库中创建 COUNTRY 表。 在 src->org.arpit.javapostsforlearning 中创建 Country.java

package org.arpit.javapostsforlearning;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

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

    @Id
    @Column(name="Country_Name")
    String countryName ;

    @OneToOne
        @JoinColumn(name="Capital_Name")
    Capital capital;

    @Column(name="Country_Population")
    long countryPopulation;

    public Country()
    {

    }

    public Country(String countryName, long countryPopulation) {
  this.countryName = countryName;
  this.countryPopulation = countryPopulation;
}

    public long getCountryPopulation() {
  return countryPopulation;
}

    public void setCountryPopulation(long countryPopulation) {
  this.countryPopulation = countryPopulation;
}

    public String getCountryName() {
        return countryName;
        }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public Capital getCapital() {
  return capital;
}

    public void setCapital(Capital capital) {
  this.capital = capital;
}
}

 

@OneToOne 注释用于在 Country 和 Capital 实体之间创建一对一的关系。 @joinColumn 用于指定用于加入实体关联的映射列。

2.Capital.java

Capital 类将用于在数据库中创建 CAPITAL 表。在 src->org.arpit.javapostsforlearning 中 创建 Capital.java 。

package org.arpit.javapostsforlearning;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="CAPITAL")
public class Capital {

@Id
@Column(name="Capital_Name")
String capitalName;

@Column(name="Capital_Population")
long capitalPopulation;

public Capital()
{

}
public Capital(String capitalName, long capitalPopulation) {
  super();
  this.capitalName = capitalName;
  this.capitalPopulation = capitalPopulation;
}

public String getCapitalName() {
  return capitalName;
}

public void setCapitalName(String capitalName) {
  this.capitalName = capitalName;
}
public long getCapitalPopulation() {
  return capitalPopulation;
}

public void setCapitalPopulation(long capitalPopulation) {
  this.capitalPopulation = capitalPopulation;
}

}

3.Hiberante.cfg.xml:

在src文件夹中创建一个名为“hibernate.cfg.xml”的 文件。





    

        
        com.microsoft.sqlserver.jdbc.SQLServerDriver
        jdbc:sqlserver://localhost:1433;database=UserInfo
        sa
        

        
        1

        
        org.hibernate.dialect.SQLServer2005Dialect

        
        thread

        
        org.hibernate.cache.NoCacheProvider

        
        true

        
        create

  
  

    

 4.Main Class:

package org.arpit.javapostsforlearning;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateMain {

public static void main(String[] args) {

Configuration configuration=new Configuration();
configuration.configure();
ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sf=configuration.buildSessionFactory(sr);
Session ss=sf.openSession();

Country countryIndia=new Country("India",50000000);
Capital capitalDelhi=new Capital("Delhi",4000000);
countryIndia.setCapital(capitalDelhi);
Country countryFrance=new Country("France",20000000);
Capital capitalParis=new Capital("Paris",1000000);
countryFrance.setCapital(capitalParis);
ss.beginTransaction();
ss.save(countryIndia);
ss.save(capitalDelhi);
ss.save(countryFrance);
ss.save(capitalParis);
ss.getTransaction().commit();
ss.close();

}

}

项目结构:

 

Hibernate 一对一映射示例_第2张图片

5.SQL output:

数据库中的 COUNTRY 表

 

数据库中的 CAPITAL 表

你可能感兴趣的:(java,java)