2017-05-12 2 views
1

데이터베이스에 데이터를 추가하려고하면이 시점에서 최대 절전 모드가 중단됩니다.엔티티를 데이터베이스에 저장하지 않습니다.

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

내 실수 일 수 있습니까?

+0

에 도움이 될 것입니다. 'Id' 클래스에 문제가 있다고 가정 해 봅시다. '@ GeneratedValue' 또는'@GeneratedValue (strategy = GenerationType.IDENTITY)'로 시도했습니다. –

+0

MySQL을 사용하고 있으며 @GeneratedValue 및 @GeneratedValue (strategy = GenerationType.IDENTITY)를 사용하려고했습니다. –

답변

0

당신이 바퀴를 재발 명하려는 것 같습니다.

코드에 중점을 둘 수도 있지만 좀 더 일반적인 제안이 있습니다.

이미 Spring과 Hibernate를 사용하고 있습니다. 대부분의 사람들이 Spring Data JPA를 사용하여 2017 년에 사용하기 때문에 그냥 사용하지 않는 것이 좋습니다.

Google을 통해 해당 주제에 대한 훌륭한 자습서를 많이 찾을 수 있습니다. 여기

공식 봄 소개 : 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/

희망

0

변경하여이 문제를 해결했습니다 :

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

아마도

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

에 누군가 : 응용 프로그램에서 사용중인 데이터베이스가 무엇인지

관련 문제