试图通过hibernate从db获取数据,它会抛出nullpointer异常

时间:2016-03-24 09:14:28

标签: hibernate jboss nullpointerexception reverse-engineering pojo

我正在使用hibernate并且我已经通过逆向工程生成了POJO,它看起来像这样 -

package tablolar;
// Generated 24.Mar.2016 10:52:26 by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Foods generated by hbm2java
 */
@Entity
@Table(name = "foods", catalog = "uretimtakipdb")
public class Foods implements java.io.Serializable {

    private Integer idfoods;
    private String foodName;
    private BigDecimal foodCost;
    private BigDecimal foodPrice;
    private Set datas = new HashSet(0);

    public Foods() {
    }

    public Foods(String foodName, BigDecimal foodCost, BigDecimal foodPrice, Set datas) {
        this.foodName = foodName;
        this.foodCost = foodCost;
        this.foodPrice = foodPrice;
        this.datas = datas;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "idfoods", unique = true, nullable = false)
    public Integer getIdfoods() {
        return this.idfoods;
    }

    public void setIdfoods(Integer idfoods) {
        this.idfoods = idfoods;
    }

    @Column(name = "foodName")
    public String getFoodName() {
        return this.foodName;
    }

    public void setFoodName(String foodName) {
        this.foodName = foodName;
    }

    @Column(name = "foodCost", precision = 10)
    public BigDecimal getFoodCost() {
        return this.foodCost;
    }

    public void setFoodCost(BigDecimal foodCost) {
        this.foodCost = foodCost;
    }

    @Column(name = "foodPrice", precision = 10)
    public BigDecimal getFoodPrice() {
        return this.foodPrice;
    }

    public void setFoodPrice(BigDecimal foodPrice) {
        this.foodPrice = foodPrice;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "foods")
    public Set getDatas() {
        return this.datas;
    }

    public void setDatas(Set datas) {
        this.datas = datas;
    }

}

我认为这是查询类 -

package tablolar;
// Generated 24.Mar.2016 10:52:27 by Hibernate Tools 3.4.0.CR1

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Home object for domain model class Foods.
 * @see tablolar.Foods
 * @author Hibernate Tools
 */
@Stateless
public class FoodsHome {

    private static final Log log = LogFactory.getLog(FoodsHome.class);

    @PersistenceContext
    private EntityManager entityManager;

    public void persist(Foods transientInstance) {
        log.debug("persisting Foods instance");
        try {
            entityManager.persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(Foods persistentInstance) {
        log.debug("removing Foods instance");
        try {
            entityManager.remove(persistentInstance);
            log.debug("remove successful");
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public Foods merge(Foods detachedInstance) {
        log.debug("merging Foods instance");
        try {
            Foods result = entityManager.merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Foods findById(Integer id) {
        log.debug("getting Foods instance with id: " + id);
        try {
            Foods instance = entityManager.find(Foods.class, id);
            log.debug("get successful");
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
}

这是主要方法 -

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Foods food = new Foods();
    FoodsHome fhome = new FoodsHome();
     food = fhome.findById(7);


    System.out.println(food.getFoodName());

}

当我试图从数据库获取数据时,它会抛出NullPointerException。问题是什么,我该如何解决?

Mar 24, 2016 10:56:25 AM tablolar.FoodsHome findById
SEVERE: get failed
java.lang.NullPointerException
    at tablolar.FoodsHome.findById(FoodsHome.java:60)
    at kostur.kosturucu.main(kosturucu.java:13)

Exception in thread "main" java.lang.NullPointerException
    at tablolar.FoodsHome.findById(FoodsHome.java:60)
    at kostur.kosturucu.main(kosturucu.java:13)

0 个答案:

没有答案