Trade实体
private EmployeeEntity employeeEntity;
@ManyToOne(cascade=CascadeType.REMOVE,fetch=FetchType.EAGER,optional=false)
public EmployeeEntity getEmployeeEntity() {
return employeeEntity;
}
public void setEmployeeEntity(EmployeeEntity employeeEntity) {
this.employeeEntity = employeeEntity;
}
Order实体:
@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,targetEntity=ComputerEntity.class,mappedBy="employeeEntity")
public List<ComputerEntity> getComputerEntityList() {
return computerEntityList;
}
public void setComputerEntityList(List<ComputerEntity> computerEntityList) {
this.computerEntityList = computerEntityList;
}
//配置参考地址:http://blog.knowsky.com/184704.htm
/**
* targetEntity指定关联的实体,这里orderList使用的是泛型,可以确定是Order,所以targetEntity可以省略
* mappedBy指定的值是指不需要维护关联关系的实体的名称,同时也是对应管理实体的一个字段属性名
* mappedBy="tradeEntity"
*/
保存时抛出的异常如下:
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.joyplus.entity.ComputerEntity.employeeEntity
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:312)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:143)
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.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:761)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 61 more
我保存时这样写的
* 批量修改绩效
* @param computerEntityList
*/
public void batchUpdateComputers(List<ComputerEntity> computerEntityList){
computerDao.saveOrUpdateAll(computerEntityList);
}
解析:
这是说我ComputerEntity里面的employeeEntity是null引起的,那我们看看表结构,我们想办法把他设置为可以为null是否就可以了呢?
我手动修改了,他它设置为可以为null了,错误依然继续,再看看主外键
显然这里是外键,外键是能不能为null我不确定,我用plsql修改外键值为nul居然是可以的,不管怎么样,这里的原因就是这个管理字段不能为null
实际上我们也不希望为null,不然我们怎么关联呢,所以修改保存代码如下
public void batchUpdateComputers(List<ComputerEntity> computerEntityList){
for (ComputerEntity computerEntity : computerEntityList) {
computerEntity.setEmployeeEntity(employeeDao.getEmployeeEntity(computerEntity.getEmployeeNo()));
}
computerDao.saveOrUpdateAll(computerEntityList);
}
这样我就成功解决这个问题了
然后前台查询显示computer时候出现json转换异常,异常如下
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72)
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23)
at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java
:86)
解析:compter中引用了employee,然后employee中也引用了compter,所以这样就造成了死循环
所以我们希望想办法不然json去转换关联字段就好了,方法如下
@AttributeOverride(name = "uuid", column = @Column(name="COMPUTER_ID"))//
@JsonIgnoreProperties (value = {"employeeEntity"})
public class ComputerEntity extends BaseEntity {
在hibernate中报IllegalArgumentException occurred calling getter of *此类异常,上网查了一下,说是是待写入数据库的值和数据库字段类型不相匹配,造成的。。。(至少这是造成错误的其中一种原因),但实际上我的并不是这个原因而导致这个异常的,代码如下:
哎,原因不清楚,反正是一对多引起的,我现在加了@JsonIgnoreProperties (value = {"computerEntityList"})就好了
另外补充说明,我做这个关联的目的是在电脑信息查询时,电脑表里面只有员工编号,我希望知道员工的姓名,所以做了这样的关联
在此我想这要比做连接查询方便的多,高档的多
我在修改电脑表时候是这样写的
public void batchUpdateComputers(List<ComputerEntity> computerEntityList){
for (ComputerEntity computerEntity : computerEntityList) {
computerEntity.setEmployeeEntity(employeeDao.getEmployeeEntity(computerEntity.getEmployeeNo()));
}
computerDao.saveOrUpdateAll(computerEntityList);
}
但是报错说session中已经有这个相同的标识,
员工表
@OneToMany(cascade={CascadeType.REFRESH},fetch=FetchType.LAZY,targetEntity=ComputerEntity.class,mappedBy="employeeEntity")
public List<ComputerEntity> getComputerEntityList() {
return computerEntityList;
}
电脑表
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER,optional=true)
public EmployeeEntity getEmployeeEntity() {
return employeeEntity;
}
public void setEmployeeEntity(EmployeeEntity employeeEntity) {
this.employeeEntity = employeeEntity;
}
原来我级联是all,可能是因为这个原因,员工表我改为refresh了,我想在电脑修改的时候,总之具体的混乱,但是出现这种问题就是级联操作
重复性加载实体的缘故啊