Hibernate - SQLGrammarException:无法在Persist上提取ResultSet

时间:2015-11-16 05:00:33

标签: java mysql hibernate jpa

我试图坚持一个实体。当我使用本机查询向我的表插入行时,它工作正常,但是当我使用EntityManager persist方法插入实体时,我得到一个错误,抱怨查询,就好像它在语法错误一样。

贝娄是我的实体班:

import javax.persistence.*;
import java.util.Date;

@Entity
public class Media {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "media_location")
    private String mediaLocation;

    @Column(name = "media_location_url")
    private boolean fromUrl = false;

    @Column(name = "date")
    private Date uploadDate;

    @Column(name = "description")
    private String description;

    public Media() {

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMediaLocation() {
        return mediaLocation;
    }

    public void setMediaLocation(String mediaLocation) {
        this.mediaLocation = mediaLocation;
    }

    public boolean isFromUrl() {
        return fromUrl;
    }

    public void setFromUrl(boolean fromUrl) {
        this.fromUrl = fromUrl;
    }

    public Date getUploadDate() {
        return uploadDate;
    }

    public void setUploadDate(Date uploadDate) {
        this.uploadDate = uploadDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

我的经理班如下:

@Stateless
public class UserManager {

    @EJB
    private BaseDao baseDao;

    public String createNewMedia_test() {

        Media media = new Media();
        media.setFromUrl(false);
        media.setDescription("adsddfsd");
        media.setMediaLocation("here");
        media.setUploadDate(new Date());

        baseDao.saveMedia(media);

        return "OK";

    }
}

BaseDao如下:

@Stateless
public class BaseDao {
    @PersistenceContext(unitName="MySqlDS")
    private EntityManager entityManager;

    public void saveMedia(Media media) {

        entityManager.persist(media);
    }
}

但是这是持续的调用,我得到了错误,在我的localhost web中,我看到堆栈跟踪以

开头
Query String:null
Stack Trace
javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

1 个答案:

答案 0 :(得分:1)

我认为您需要在Hibernate 事务中调用persist()。尝试将您的电话打包到交易中的persist(),如下面的代码段所示:

@Stateless
public class BaseDao {
    @PersistenceContext(unitName="MySqlDS")
    private EntityManager entityManager;

    public void saveMedia(Media media) {

        entityManager.getTransaction().begin();
        entityManager.persist(media);
        entityManager.getTransaction().commit();
    }
}