不将实体持久化到db

时间:2017-05-12 23:34:10

标签: java hibernate spring-mvc

当我尝试向数据库添加数据时,hibernate就会挂起。

Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?

域类

package org.jazzteam.domain.commentary;

import org.jazzteam.domain.id.Id;
import org.jazzteam.domain.event.Event;
import org.jazzteam.domain.user.SimpleUser;
import org.jazzteam.domain.user.User;

import javax.persistence.*;

/**
* @author Yura
* @version 1.0
*/
@Entity
@Table
public class Commentary extends Id {
@OneToOne(cascade = CascadeType.ALL)
private SimpleUser author;
private String description;
/*@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Event event;*/
private int rating;

public Commentary(){

}

public Commentary(SimpleUser author, String description, Event event, int rating){
    this.author = author;
    this.description = description;
    /*this.event = event;*/
    this.rating = rating;
}

public void setAuthor(SimpleUser author) {
    this.author = author;
}

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

/*public void setEvent(Event event) {
    this.event = event;
}*/

public void setRating(int rating) {
    this.rating = rating;
}

public User getAuthor() {
    return author;
}

public String getDescription() {
    return description;
}

/*public Event getEvent() {
    return event;
}*/

public int getRating() {
    return rating;
}
}

DAO

package org.jazzteam.dao.commentary;

import org.hibernate.SessionFactory;
import org.jazzteam.domain.commentary.Commentary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Created by Yura on 14.04.2017.
*/
@Repository
public class CommentaryDaoImpl implements CommentaryDao<Commentary> {
@Autowired
SessionFactory sessionFactory;

@Override
public void persist(Commentary entity) {
    sessionFactory.getCurrentSession().persist(entity);
}

@Override
public void update(Commentary entiry) {
    sessionFactory.getCurrentSession().update(entiry);
}

@Override
public void delete(Commentary entity) {
    sessionFactory.getCurrentSession().delete(entity);
}

@Override
public Commentary findById(int id) {
    return sessionFactory.getCurrentSession().get(Commentary.class, id);
}

@Override
public List<Commentary> findAll() {
    return sessionFactory.getCurrentSession().createQuery("from Commentary ").list();
}
}

服务类

package org.jazzteam.service.commentary;

import org.jazzteam.dao.commentary.CommentaryDao;
import org.jazzteam.domain.commentary.Commentary;
import org.jazzteam.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* Created by Yura on 14.04.2017.
*/

@org.springframework.stereotype.Service
public class CommentaryService implements Service<Commentary> {
@Autowired
CommentaryDao commentaryDao;

@Transactional
public void persist(Commentary entity) {
    commentaryDao.persist(entity);
}

@Transactional
public void update(Commentary entity) {
    commentaryDao.update(entity);
}

@Transactional
public void delete(int id) {
    commentaryDao.delete(commentaryDao.findById(id));
}

@Transactional
public Commentary findById(int id) {
    return (Commentary) commentaryDao.findById(id);
}

@Transactional
public List<Commentary> findAll() {
    return commentaryDao.findAll();
}
}

我正在尝试添加:

Commentary commentary = new Commentary(simpleUserService.findById(idAuthor),
            comment,
            eventService.findById(idEvent),
            rate);
commentaryService.persist(commentary);

按类ID生成的ID:     包org.jazzteam.domain.id;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.MappedSuperclass;

/**
* @author Yura
* @version 1.0
*/
@MappedSuperclass
public abstract class Id {
@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected int id;

public Id() {
}

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

public int getId() {

    return id;
}
}

可能是我的错误?

2 个答案:

答案 0 :(得分:0)

看起来你正试图重新发明轮子。

我们可能会密切关注您的代码,但我有一个更一般的建议。

您已经使用过Spring和Hibernate。为什么不使用它,因为大多数人在2017年使用它 - 使用Spring Data JPA。

您可以谷歌搜索并找到很多关于该主题的精彩教程。

以下是春季官方介绍:https://spring.io/guides/gs/accessing-data-jpa/

带有一些示例的GitHub项目:https://github.com/spring-projects/spring-data-examples

和参考:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

希望有所帮助:)

欢呼声

答案 1 :(得分:0)

通过更改:

解决了这个问题
@Override
public void persist(Commentary entity) {
    sessionFactory.getCurrentSession().persist(entity);
}

@Override
public void persist(Commentary entity) {
     sessionFactory.getCurrentSession().save(entity);
}

也许对某人有用:)