问题描述
我正在获取weired异常并且无法追踪为何如此。请帮助我。
我只是使用OnetoOne映射和PrimarykeyJoinColumn属性。
@Entity
@Table(name =mediashow_user1)
public class UserVO实现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;
私人诠释电话;
私人时间戳cur_timestamp = null;
私人时间戳lastvisited_timestamp = null;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private AddressVO addressVO;
$ b @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;
私人字符串zipcode = null;
private String landmark = null;
........
}
交易交易=空;
尝试{
transaction = session.beginTransaction();
AnnotationConfiguration conf =(new AnnotationConfiguration())。configure();
新的SchemaExport(conf).create(true,false);
UserVO user = new UserVO();
user.setEmail([email protected]);
user.setFname(Rahul10);
user.setMname(Bapusaheb10);
user.setLname(Bandgar10);
user.setUname(rahul);
user.setPasswd(rahul);
user.setSerctanswer(baramati);
user.setSerctquestion(你的出生地名是什么?);
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
drop
外键FK5FD5BCE8801495D
drop table如果存在mediashow_address1
drop table if exists存在mediashow_user1
$ b $ create table mediashow_address1(
empid bigint不为null,
landmark varchar(255),
street varchar(255),
zipcode varchar(255),
主键(empid)
)
创建表mediashow_user1(
empid bigint不为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),
电话整数非空,
uname varchar(255),
pr imary key(empid)
)
alter table mediashow_user1
add index FK5FD5BCE8801495D(empid),
add constraint FK5FD5BCE8801495D
外键(empid)
引用mediashow_address1(EMPID)
在线程异常在org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue 主 显示java.lang.NullPointerException
(AbstractEntityTuplizer.java:521)
。在在org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:100)
org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3867)
。在组织。 hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
处org.hibernate.event org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
。 def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)$ b org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
$ b 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)
在org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
在org.hibernate.engine.Cascade.cascadeAssociation(Cascade。
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)在org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
在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)
。在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
在org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
。在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
在org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
在org.hiber nate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at com.imgshop.run.TestAddIntoLogin.main( TestAddIntoLogin.java:49)
经过一些实验后自己解决。这里要记住的一件重要事情是,当您为ID生成器使用 foreign
策略时,关系不能是单向的。在你的情况下,你需要做两个改变(见下文)。基本上你已经为AddressVO @OneToOne(cascade = CascadeType.ALL)
设置级联,当你尝试保存UserVO会触发AddressVO的类似操作。但是对于AddressVO,您将Id生成器策略设置为'foreign',其属性为 userVO
。但是您从不在AddressVO中设置属性 userVO
,因此它会得到空id,因此是异常。为了使这项工作为你做,需要做以下的改变:
$ b $ 1)将UserVO属性(getter / setters)添加到AddressVo,然后保存userVo将此对象添加到addressVO中事务 address.setUserVo(user);
然后调用 session.save(user);
2)编辑您的数据库模式以反映addressVO中的共享主键,因为外键约束位于AddressVo中而不是UserVO中。即改变以下内容:
pre $ lt; code> alter table mediashow_user1
add index FK5FD5BCE8801495D(empid),
add constraint FK5FD5BCE8801495D
外键(empid)
引用mediashow_address1(empid)
p>
alter table mediashow_address1
添加索引FK5FD5BCE8801495D(empid),
添加约束FK5FD5BCE8801495D
外键( empid)
引用mediashow_user1(empid)
如果您想使这项工作适用于 Unidirection 一对一关系,那么你应该自己处理级联操作。基本上你应该做下面的事情:1)去掉外键策略(只需在AddressVO中使用@Id和@column注释来代替@GeneratedValue)。
$ b 2)删除UserVO中AddressVo的级联选项(因为这会触发AddressVO上的类似操作,并且id仍然未知)
3)在保存此地址之前设置AddressVo的empId。您应该使用您在保存userVo时获得的id。
Long id =(Long)session.save(user);
address.setEmpid(id);
session.save(地址)
基本上你应该处理保存/更新/删除操作对于地址也是如此。
希望这个解释有助于:)
I am getting weired exception and not able to trace why it so. Please help me here.
I am just using OneToOne mapping with PrimarykeyJoinColumn property.
@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;
}
..................
another 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("[email protected]");
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();
}
and I am getting following exception while running the above code.
alter table mediashow_user1 drop foreign key 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)
I faced similar situation and resolved it myself after some experimentation. An important thing to remember here is that when you use foreign
strategy for an ID generator the relationship can't be unidirectional. In your case you need to make two changes(see below).Basically you have set cascade to all for AddressVO @OneToOne(cascade=CascadeType.ALL)
and when you try to save the UserVO this will trigger similar operation for AddressVO. But for AddressVO you set the Id generator strategy as 'foreign' with property userVO
. But you never set the property userVO
in AddressVO so it gets null id and hence the exception. To make this work for you need to do the following changes
1) Add userVO property (getter/setters) to AddressVo and before saving userVo add this object to addressVO in you transaction address.setUserVo(user);
and then call session.save(user);
2)Edit your db schema to reflect the shared primarykey in addressVO as the foreign key constraint is in AddressVo not in UserVO. i.e., change the following
alter table mediashow_user1
add index FK5FD5BCE8801495D (empid),
add constraint FK5FD5BCE8801495D
foreign key (empid)
references mediashow_address1 (empid)
to
alter table mediashow_address1
add index FK5FD5BCE8801495D (empid),
add constraint FK5FD5BCE8801495D
foreign key (empid)
references mediashow_user1 (empid)
If you want to make this work for Unidirection one-to-one relation ship then you should handle the cascade operations yourself. Basically you should do the following
1)Remove the foreign key strategy (Just use @Id and @column annotations for empId in AddressVO with out any @GeneratedValue).
2)Remove the cascade options for AddressVo in your UserVO (as this will trigger similar operations on AddressVO and the id is still not known)
3)Set the empId for AddressVo before saving this address. You should use the id that you obtain when you save userVo.
Long id= (Long)session.save(user);
address.setEmpid(id);
session.save(address)
basically you should handle the save/update/delete operations for addresss too.
Hope this explanation helps :)
这篇关于Hibernate代码... onetoone映射中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!