Java 实体字段集上的Hibernate NullPointerException(更新)
我在Hibernate 4.3.0 Final中遇到了一个无法解决的问题。这是令人费解的,因为我正在其他几个地方运行类似的代码,这些代码运行得很好。下面的代码正在抛出一个Java 实体字段集上的Hibernate NullPointerException(更新),java,hibernate,Java,Hibernate,我在Hibernate 4.3.0 Final中遇到了一个无法解决的问题。这是令人费解的,因为我正在其他几个地方运行类似的代码,这些代码运行得很好。下面的代码正在抛出一个java.lang.NullPointerException。它在一个web服务中运行,使用的是使用Glassfish和JavaDB的NetBeans设置 异常在item.setRoomId(-1)之后抛出: characterId,characterFirstName,roomId,k,变量从上面传入;我通过调试验证了它们都具
java.lang.NullPointerException
。它在一个web服务中运行,使用的是使用Glassfish和JavaDB的NetBeans设置
异常在item.setRoomId(-1)之后抛出代码>:
characterId
,characterFirstName
,roomId
,k,变量从上面传入;我通过调试验证了它们都具有有效数据类型的值;Items表中还有一条有效记录
Query itemsQuery = em.createNamedQuery("Items.findByRoomIdByName");
itemsQuery.setParameter("roomId", roomId);
itemsQuery.setParameter("name", itemName);
Items item = null;
if (itemsQuery.getResultList().isEmpty()) {
throw new UnableToIdentifyException("Item does not exist! You cannot get an item that does not exist. Did you already pick it up?");
}
else {
item = (Items) itemsQuery.getSingleResult();
Boolean isStuck = item.getIsUnmovable();
if (isStuck) {
//Item stuck
notifyItemGetViewers(characterId, characterFirstName, roomId, itemName, isStuck);
}
else {
//Pick up item
try {
item.setRoomId(-1);
item.setCharacterId(characterId);
item.setStoreId(-1);
}
catch (Exception e) {
logger.severe("Wrapped Exection Caught: Exception: " + e.toString() + " Error Message: " + e.getMessage());
}
em.persist(item);
em.flush();
notifyItemGetViewers(characterId, characterFirstName, roomId, itemName, isStuck);
}
}
实体类(Items.java)
项目表中的数据:
将值(1,-1,-1,'剑','普通钢剑',-1,0.0,-1,假,-1,-1,-1,假,-1,-1,-1,假)插入mit_项目(roomid,characterid,storeid,name,description,type,Value,bodylocation,isunmovable,acbonus,useTimeDelay,damageDice,damageDiceSides,damageType,lightprovided)
有趣的是,在抛出异常之后,数据库显示roomId
实际上已设置为-1
。但是,其他字段尚未更新。如果我对设置字段(-1)
方法重新排序,异常仍然会在第一个方法上触发。而且,最初try-catch块不在那里。我必须添加它才能查看根本原因异常,因为此方法具有@Transactional
注释,该注释将异常包装在一个不显示根本原因异常的常规回滚异常中。谢谢,RC解决了此问题。propertySupport返回null。不知道为什么,它通常有一个很长的数字转换成一个字符串。我最终从Items.java实体类中删除了propertySupport,解决了这个问题
我能感觉到为什么在这种情况下失败,而不是其他情况,是因为我在Items表中手动插入了一条记录以进行测试,并且我将propertySupport字段保留为空。因此,显然,如果实现了propertySupport,则无法更新具有空propertySupport字段的记录中的值,我猜只有通过实体bean以外的方式插入数据时才会发生这种情况
再次感谢你Sam您正在捕获异常
,然后代码仍在运行,您是否收到任何异常?完整堆栈跟踪请猜测属性支持
是否为空根本原因是NullPointerException。完全stacktrace是无用的,因为@Transactional注释包装在它自己的RollbackException中,尽管引起异常的语句是try{block中的第一个语句,即使我重新排序并将setStoreId(-1)放在顶部,它仍然会将其抛出,setCharacterId()也是如此。关于propertySupport为null,您可能是对的,但这将是Hibernate4.3.0Final中的一个错误,我不确定如何调试类似的东西。在setXXX
检查propertySupport
值时,您将确定。另外请注意propertySupport
不能是列,也不能是插入的内容在构造函数中进行tantiate。
package org.tav.mit;
import java.beans.*;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "mit_items")
@NamedQueries({
@NamedQuery(name="Items.findByItemId",
query="SELECT i FROM Items i WHERE i.itemId = :itemId"),
@NamedQuery(name="Items.findByRoomIdByName",
query="SELECT i FROM Items i WHERE i.roomId = :roomId AND i.name = :name"),
@NamedQuery(name="Items.findByRoomId",
query="SELECT i FROM Items i WHERE i.roomId = :roomId"),
@NamedQuery(name="Items.findByCharacterId",
query="SELECT i FROM Items i WHERE i.characterId = :characterId"),
@NamedQuery(name="Items.findByStoreId",
query="SELECT i FROM Items i WHERE i.storeId = :storeId"),
@NamedQuery(name="Items.findByName",
query="SELECT i FROM Items i WHERE i.name = :name")
})
public class Items implements Serializable {
public static final String PROP_ITEMID = "itemIdProperty";
public static final String PROP_ROOMID = "roomIdProperty";
public static final String PROP_CHARACTERID = "characterIdProperty";
public static final String PROP_STOREID = "storeIdProperty";
public static final String PROP_NAME = "nameProperty";
public static final String PROP_DESCRIPTION = "descriptionProperty";
public static final String PROP_TYPE = "typeProperty";
public static final String PROP_WORTH = "worthProperty";
public static final String PROP_BODYLOCATION = "bodyLocationProperty";
public static final String PROP_ISUNMOVABLE = "isUnmovableProperty";
public static final String PROP_ACBONUS = "acBonusProperty";
public static final String PROP_USETIMEDELAY = "useTimeDelayProperty";
public static final String PROP_DAMAGEDICE = "damageDiceProperty";
public static final String PROP_DAMAGEDICESIDES = "damageDiceSidesProperty";
public static final String PROP_DAMAGETYPE = "damageTypeProperty";
public static final String PROP_LIGHTPROVIDED = "lightProvidedProperty";
@Id
@GeneratedValue
private Integer itemId;
@Column()
private Integer roomId = -1;
@Column()
private Integer characterId = -1;
@Column()
private Integer storeId = -1;
@Column(length = 128, nullable = false)
private String name;
@Column(length = 2048, nullable = true)
private String description;
@Column()
private Integer type = -1;
@Column()
private Double worth = 0.0;
@Column()
private Integer bodyLocation = -1;
@Column(nullable = false)
private Boolean isUnmovable = false;
@Column()
private Integer acBonus = -1;
@Column()
private Integer useTimeDelay = -1;
@Column()
private Integer damageDice = -1;
@Column()
private Integer damageDiceSides = -1;
@Column()
private Integer damageType = -1;
@Column(nullable = false)
private Boolean lightProvided = false;
@Column(length = 2048)
private PropertyChangeSupport propertySupport;
public Items()
{
propertySupport = new PropertyChangeSupport(this);
}
public int getItemId() {
return itemId;
}
public void setItemId(Integer itemId) {
Integer oldValue = this.itemId;
this.itemId = itemId;
propertySupport.firePropertyChange(PROP_ITEMID, oldValue, itemId);
}
public int getRoomId() {
return roomId;
}
public void setRoomId(Integer roomId) {
Integer oldValue = this.roomId;
this.roomId = roomId;
propertySupport.firePropertyChange(PROP_ROOMID, oldValue, roomId);
}
public int getCharacterId() {
return characterId;
}
public void setCharacterId(Integer characterId) {
Integer oldValue = this.characterId;
this.characterId = characterId;
propertySupport.firePropertyChange(PROP_CHARACTERID, oldValue, characterId);
}
public int getStoreId() {
return storeId;
}
public void setStoreId(Integer storeId) {
Integer oldValue = this.storeId;
this.storeId = storeId;
propertySupport.firePropertyChange(PROP_STOREID, oldValue, storeId);
}
public String getName() {
return name;
}
public void setName(String name) {
String oldValue = this.name;
this.name = name;
propertySupport.firePropertyChange(PROP_NAME, oldValue, name);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
String oldValue = this.description;
this.description = description;
propertySupport.firePropertyChange(PROP_DESCRIPTION, oldValue, description);
}
public int getType() {
return type;
}
public void setType(Integer type) {
Integer oldValue = this.type;
this.type = type;
propertySupport.firePropertyChange(PROP_TYPE, oldValue, type);
}
public double getWorth() {
return worth;
}
public void setWorth(double worth) {
double oldValue = this.worth;
this.worth = worth;
propertySupport.firePropertyChange(PROP_WORTH, oldValue, worth);
}
public int getBodyLocation() {
return bodyLocation;
}
public void setBodyLocation(Integer bodyLocation) {
Integer oldValue = this.bodyLocation;
this.bodyLocation = bodyLocation;
propertySupport.firePropertyChange(PROP_BODYLOCATION, oldValue, bodyLocation);
}
public boolean getIsUnmovable() {
return isUnmovable;
}
public void setIsUnmovable(boolean isUnmovable) {
boolean oldValue = this.isUnmovable;
this.isUnmovable = isUnmovable;
propertySupport.firePropertyChange(PROP_ISUNMOVABLE, oldValue, isUnmovable);
}
public int getAcBonus() {
return acBonus;
}
public void setAcBonus(Integer acBonus) {
Integer oldValue = this.acBonus;
this.acBonus = acBonus;
propertySupport.firePropertyChange(PROP_ACBONUS, oldValue, acBonus);
}
public int getUseTimeDelay() {
return useTimeDelay;
}
public void setUseTimeDelay(Integer useTimeDelay) {
Integer oldValue = this.useTimeDelay;
this.useTimeDelay = useTimeDelay;
propertySupport.firePropertyChange(PROP_USETIMEDELAY, oldValue, useTimeDelay);
}
public int getDamageDice() {
return damageDice;
}
public void setDamageDice(Integer damageDice) {
Integer oldValue = this.damageDice;
this.damageDice = damageDice;
propertySupport.firePropertyChange(PROP_DAMAGEDICE, oldValue, damageDice);
}
public int getDamageDiceSides() {
return damageDiceSides;
}
public void setDamageDiceSides(Integer damageDiceSides) {
Integer oldValue = this.damageDiceSides;
this.damageDiceSides = damageDiceSides;
propertySupport.firePropertyChange(PROP_DAMAGEDICESIDES, oldValue, damageDiceSides);
}
public int getDamageType() {
return damageType;
}
public void setDamageType(Integer damageType) {
Integer oldValue = this.damageType;
this.damageType = damageType;
propertySupport.firePropertyChange(PROP_DAMAGETYPE, oldValue, damageType);
}
public boolean getLightProvided() {
return lightProvided;
}
public void setLightProvided(boolean lightProvided) {
boolean oldValue = this.lightProvided;
this.lightProvided = lightProvided;
propertySupport.firePropertyChange(PROP_LIGHTPROVIDED, oldValue, lightProvided);
}
@Override
public int hashCode() {
int hash = 0;
hash += (itemId != -1 ? itemId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Items)) {
return false;
}
Items other = (Items) object;
if ((this.itemId == -1 && other.itemId != -1) || (this.itemId != -1 &&
!((this.itemId) == (other.itemId)))) {
return false;
}
return true;
}
}