(丈夫和夫妻的一对一关系)
package org.hibernate.demo.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import sun.nio.cs.Surrogate.Generator;
@Entity
public class Husband implements Serializable
{
private Integer id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public Integer getId()
{
return id;
}
@Entity
public class Husband implements Serializable
{
private Integer id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
{
this.id = id;
}
public String getName()
{
return name;
}
{
return name;
}
public void setName(String name)
{
this.name = name;
}
//此处注解是一对一关系
@OneToOne
@JoinColumn(name="wife")
public Wife getWife()
{
return wife;
}
{
this.name = name;
}
//此处注解是一对一关系
@OneToOne
@JoinColumn(name="wife")
public Wife getWife()
{
return wife;
}
public void setWife(Wife wife)
{
this.wife = wife;
}
{
this.wife = wife;
}
public Husband()
{
// TODO Auto-generated constructor stub
}
{
// TODO Auto-generated constructor stub
}
}
------------------------------------------------------------------------------------------------------------------------------------------
package org.hibernate.demo.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Wife implements Serializable
{
private Integer id;
private String name;
@Id
@GeneratedValue
public Integer getId()
{
return id;
}
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Wife implements Serializable
{
private Integer id;
private String name;
@Id
@GeneratedValue
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
{
this.id = id;
}
public String getName()
{
return name;
}
{
return name;
}
public void setName(String name)
{
this.name = name;
}
{
this.name = name;
}
public Wife()
{
// TODO Auto-generated constructor stub
}
{
// TODO Auto-generated constructor stub
}
}
--------------------上面是注解方式的one-to-one----------------------------
---------------------下面是xml方式-------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.demo.entity">
<class name="Wife">
<id name="id">
<generator class="identity"></generator>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
<class name="Wife">
<id name="id">
<generator class="identity"></generator>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
----
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.demo.entity">
<class name="Husband">
<id name="id">
<generator class="identity" />
</id>
<property name="name" />
<!-- 在xml中配置一对一的关系的时候,用的是many-to-one,unique=“true”就保证了一对一的关系 -->
<many-to-one name="wife" column="wife" unique="true"></many-to-one>
</class>
</hibernate-mapping>
<class name="Husband">
<id name="id">
<generator class="identity" />
</id>
<property name="name" />
<!-- 在xml中配置一对一的关系的时候,用的是many-to-one,unique=“true”就保证了一对一的关系 -->
<many-to-one name="wife" column="wife" unique="true"></many-to-one>
</class>
</hibernate-mapping>