实体字段集(UPDATE)上的Hibernate NullPointerException

时间:2013-12-24 04:07:27

标签: java hibernate

我遇到了Hibernate 4.3.0Final的问题,我无法解决。令人费解的是因为我在其他几个地方运行类似的代码,这些代码运行正常。以下代码抛出java.lang.NullPointerException。它使用Glassfish和JavaDB使用NetBeans设置在Web服务中运行。

item.setRoomId(-1);之后抛出异常: characterIdcharacterFirstNameroomId,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)

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;
    }
}

项目表中的数据: INSERT INTO mit_items(roomid,characterid,storeid,name,description,type,worth,bodylocation,isunmovable,acbonus,useTimeDelay,damageDice,damageDiceSides,damageType,lightprovided)VALUES(1,-1,-1,'sword','plain钢剑',-1,0.0,-1,假,-1,-1,-1,-1,-1,假);

一个有趣的事情是,在异常抛出后,数据库显示roomId实际上已设置为-1。但是其他字段尚未更新。如果我重新排序setFields(-1)方法,则异常仍会在第一个方法上触发。此外,最初的try-catch块不在那里。我必须添加它才能查看根本原因异常,因为此方法具有@Transactional注释,该注释将异常包装在不显示根本原因异常的常规RollbackException中。

1 个答案:

答案 0 :(得分:0)

谢谢,RC解决了这个问题。 propertySupport返回null。不知道为什么,它通常有一个长数字转换为String。我最终从Items.java实体类中删除了propertySupport,修复了问题。

我感觉为什么它在这种情况下失败了而不是其他因为我在Items表中手动插入记录进行测试,我将propertySupport字段保留为null。显然,如果你实现了propertySupport,你就无法更新具有null propertySupport字段的记录中的值,我想只有在你通过实体bean以外的方式插入数据时才会出现这种情况。

再次感谢RC! - 山姆