在onetoone映射中的Hibernate代码...异常

时间:2011-10-25 16:32:08

标签: hibernate one-to-one

我收到了异常的例外情况,但无法追查其原因。请帮帮我。

我只是使用OneToOne映射和PrimarykeyJoinColumn属性。

@Entity
@Table(name="mediashow_user1")
public class UserVO implements Serializable{

private static final long serialVersionUID = 6066636545309839156L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long empid ;

private String email = null;
private String fname = null ;
private String lname = null ;
private String mname = null ;
private String uname = null ;
private String passwd = null ;
private String serctquestion = null ;
private String serctanswer = null ;
private int telephone;
private Timestamp cur_timestamp = null ;
private Timestamp lastvisited_timestamp = null ;

@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
private AddressVO addressVO;


@Column(name="empid")
public Long getEmpid() {
    return empid;
}
    ..................

另一个AddressVO.java

@Entity
@Table(name="mediashow_address1")
@GenericGenerator(name="foreign", strategy = "foreign", parameters={
@Parameter(name="property", value="userVO")
})

public class AddressVO implements Serializable {


private static final long serialVersionUID = -96886746528894662L;

@Id
@GeneratedValue(generator="foreign")
private Long empid;

private String street = null ;   
private String zipcode = null ; 
private String landmark = null ;
    ........

   }


    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();

        AnnotationConfiguration conf = (new      AnnotationConfiguration()).configure();
        new SchemaExport(conf).create(true, false);

        UserVO user = new UserVO();
        user.setEmail("abc@gmail.com");
        user.setFname("Rahul10");
        user.setMname("Bapusaheb10");
        user.setLname("Bandgar10");

        user.setUname("rahul");
        user.setPasswd("rahul");

        user.setSerctanswer("baramati");
        user.setSerctquestion("What is your birth place name?");

        AddressVO address = new AddressVO();
        address.setLandmark("Landmark5");
        address.setStreet("street4");
        address.setZipcode("234344");
        int i = 9850;
        user.setTelephone(i);
        user.setAddressVO(address);

        Long id= (Long)session.save(user);
        System.out.println("ID::"+id);

        transaction.commit();
    } catch (HibernateException e) {
        System.out.println("Exception:"+e.getMessage());
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

我在运行上面的代码时遇到异常。

alter table mediashow_user1         下降         外键FK5FD5BCE8801495D

drop table if exists mediashow_address1

drop table if exists mediashow_user1

create table mediashow_address1 (
    empid bigint not null,
    landmark varchar(255),
    street varchar(255),
    zipcode varchar(255),
    primary key (empid)
)

create table mediashow_user1 (
    empid bigint not null auto_increment,
    cur_timestamp datetime,
    email varchar(255),
    fname varchar(255),
    lastvisited_timestamp datetime,
    lname varchar(255),
    mname varchar(255),
    passwd varchar(255),
    serctanswer varchar(255),
    serctquestion varchar(255),
    telephone integer not null,
    uname varchar(255),
    primary key (empid)
)

alter table mediashow_user1 
    add index FK5FD5BCE8801495D (empid), 
    add constraint FK5FD5BCE8801495D 
    foreign key (empid) 
    references mediashow_address1 (empid)

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:521)
    at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3867)
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:100)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
    at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
    at com.imgshop.run.TestAddIntoLogin.main(TestAddIntoLogin.java:49)

2 个答案:

答案 0 :(得分:5)

我遇到了类似的情况,经过一些实验后自己解决了。这里要记住的一件重要事情是,当您对ID生成器使用foreign策略时,关系不能是单向的。在您的情况下,您需要进行两处更改(见下文)。基本上,您已为AddressVO @OneToOne(cascade=CascadeType.ALL)设置了全部级联,当您尝试保存UserVO时,这将触发AddressVO的类似操作。但是对于AddressVO,您将Id生成器策略设置为具有属性userVO的“外部”。但是你永远不会在AddressVO中设置属性userVO,因此它会获得null id,因此也会出现异常。为此,您需要进行以下更改

1)将userVO属性(getter / setters)添加到AddressVo,然后在保存userVo之前将此对象添加到事务address.setUserVo(user);中的addressVO,然后调用session.save(user);

2)编辑db模式以反映addressVO中的共享主键,因为外键约束在AddressVo中而不在UserVO中。即,更改以下

alter table mediashow_user1 
add index FK5FD5BCE8801495D (empid), 
add constraint FK5FD5BCE8801495D 
foreign key (empid) 
references mediashow_address1 (empid)

alter table mediashow_address1 
add index FK5FD5BCE8801495D (empid), 
add constraint FK5FD5BCE8801495D 
foreign key (empid) 
references mediashow_user1 (empid)

如果您希望将此功能用于 Unidirection 一对一关系,那么您应该自己处理级联操作。基本上你应该做以下

1)删除外键策略(只需在AddressVO中为empId使用@Id和@column注释,不带任何@GeneratedValue)。

2)在UserVO中删除AddressVo的级联选项(因为这将触发AddressVO上的类似操作并且id仍然未知)

3)在保存此地址之前,为AddressVo设置empId。您应该使用保存userVo时获得的ID。

Long id= (Long)session.save(user);
address.setEmpid(id);
session.save(address)

基本上你应该处理地址的保存/更新/删除操作。

希望这个解释有助于:)

答案 1 :(得分:0)

在代码中进行此更改:

 @GenericGenerator(name="foreign", strategy = "foreign", parameters={
 @Parameter(name="property", value="user")
 })

我们需要在value参数的参数注释中传递类的对象而不是类名。 因此,不应该有value =“userVO”,而应该是value =“user”

请尝试让我知道它是否有效