我无法解决Hibernate 4.3.0Final的问题。令人困惑,因为我在其他几个地方都运行了类似的代码,效果很好。以下代码抛出java.lang.NullPointerException
。它通过使用Glassfish和JavaDB的NetBeans设置在Web服务中运行。
后抛出异常item.setRoomId(-1);
:characterId
,characterFirstName
,roomId
,K,变量被从上方通过; 我已经通过调试验证了它们都具有有效数据类型的值;项目表中也有一个有效的记录。
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);
}
}
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;
}
}
项目表中的数据:插入mit_items(roomid,characterid,storeid,名称,描述,类型,价值,身体位置,不可移动,字母,useTimeDelay,damageDice,damageDiceSides,damageType,lightprovided)VALUES(1 、、-1, -1,``剑'',``普通钢剑'',-1、0.0,-1,假,-1,-1,-1,-1,-1,假);
一件有趣的事情是,在引发异常之后,数据库显示roomId
实际上已将设置为-1
。但是,其他字段尚未更新。如果我对setFields(-1)
方法重新排序,则异常仍会在第一个方法上触发。同样,最初的try-catch块不存在。我必须添加它才能查看根本原因异常,因为此方法具有@Transactional
将异常包装在RollbackException
不显示根本原因异常的常规中的注释。
谢谢,RC解决了这个问题。propertySupport返回null。不知道为什么,它通常在其中将Long数字转换为String。我最终从Items.java实体类中删除了属性支持,从而解决了该问题。
我感觉为什么在这种情况下失败了,而在其他情况下却失败了,是因为我在Items表中手动插入了一条记录进行测试,而我将propertySupport字段保留为空。因此,显然,如果实现了propertySupport,则无法更新具有null propertySupport字段的记录中的值,我猜这只有在通过实体bean以外的其他方式插入数据时才会发生。
再次感谢RC!–山姆
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句