日期:2014-05-17  浏览次数:20649 次

hiberante延迟加载失效
    最近在学习hiberante,遇到了一个问题,关于一对一延迟加载失效的问题,到现在还没解决。以下是代码:Person.java
package org.javatribe.annotation.entity.onetoone;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import org.hibernate.annotations.GenericGenerator;
@Entity
public class Person implements Serializable
{
private static final long serialVersionUID = 1L;
private String id;
private String name;
private int age;
private IdCard idCard;
public Person()
{
}
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
@Id
@GeneratedValue(generator="uuid")
@GenericGenerator(name="uuid",strategy="uuid")
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
@Column(length=20)
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@OneToOne(optional=false,cascade={CascadeType.REMOVE,CascadeType.PERSIST},fetch=FetchType.LAZY)
@JoinColumn(name="idCard_id")
public IdCard getIdCard()
{
return idCard;
}
public void setIdCard(IdCard idCard)
{
this.idCard = idCard;
}
}

IdCard.java:
package org.javatribe.annotation.entity.onetoone;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.Proxy;
/**
 * @author zxp
 * @datetime 2013-11-19 上午11:16:23
 * @description 身份证
 */
@Entity
public class IdCard implements Serializable
{
private String id;
private String idno;
private Person person;
public IdCard()
{
}
public IdCard(String idno)
{
this.idno = idno;
}
@Id
@GeneratedValue(generator="uuid")
@GenericGenerator(name="uuid",strategy="uuid")
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
@Column(length=20)
public