데이터베이스에 데이터를 추가하려고하면이 시점에서 최대 절전 모드가 중단됩니다.엔티티를 데이터베이스에 저장하지 않습니다.
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 : package 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;
}
}
내 실수 일 수 있습니까?
에 도움이 될 것입니다. 'Id' 클래스에 문제가 있다고 가정 해 봅시다. '@ GeneratedValue' 또는'@GeneratedValue (strategy = GenerationType.IDENTITY)'로 시도했습니다. –
MySQL을 사용하고 있으며 @GeneratedValue 및 @GeneratedValue (strategy = GenerationType.IDENTITY)를 사용하려고했습니다. –