我有这堂课,我想坚持上这堂课,但是我遇到错误。
@Entity
@Table(name = "Participant")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name="user_id")
User user;
@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name="meet_id")
Meet meet;
@Enumerated(EnumType.STRING)
@Column(name = "rol")
Rol rol;
}
当我尝试保存时,出现此错误:
2019-05-20 22:03:29.637 ERROR 17416 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exceptio
n [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.example.demo.model.User; nested exceptio
n is org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.demo.model.User] with root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.demo.model.User
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:765) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
如何解决我的问题?
在我的服务中,我做到了
public class MeetServiceImpl {
@Autowired
MeetRepository meetRepository;
@Autowired
UserRepository userRepository;
@Autowired
ParticipantRepository participantRepository;
static Logger log = Logger.getLogger(MeetServiceImpl.class.getName());
public Meet createMeet(Long idUser, Meet meet) {
Meet currentMeet = meetRepository.findByName(meet.getName());
User user = userRepository.findById(idUser);
Participant participant = new Participant();
participant.setRol(Rol.OWNER);
Meet meet1 = new Meet();
meet1.setName("nuevo");
meet1.setState(State.start);
participant.setMeet(meet1);
participant.setUser(user);
participantRepository.save(participant);
return meetRepository.save(meet);
}
我不明白问题是什么,我做错了什么,这似乎没有逻辑
额外:这是型号, 下面我展示了有关我使用的类的模型
@Entity
@Table(name = "Meet")
public class Meet implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(unique = true, name = "name")
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "state")
private State state;
和班见面
@Entity
@Table(name = "User")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
@Column(unique = true, name = "email")
private String email;
@Enumerated(EnumType.STRING)
@Column(name = "life")
Life life;
当我评论ID时
// @ GeneratedValue(strategy = GenerationType.AUTO)
我收到此错误
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'
答案 0 :(得分:0)
您的创建方法似乎不是事务性的。您可以在类或方法的顶部添加@Transactional吗?
答案 1 :(得分:0)
您已经在对象Meet上进行了两次插入:
participantRepository.save(participant);
return meetRepository.save(meet);
问题是: 1-第一行(1)保存参与者和会议,因为您已经在 参加者类别:
@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name="meet_id")
Meet meet;
因此,只要您保存与会议相关的参与者,会议便会保存 自动
2-当您评论此行时:// @ GeneratedValue(strategy = GenerationType.AUTO) id不会生成,所以我们有两个相遇,在插入状态mnt由jpa生成,id为0。所以数据库说:键'PRIMARY'的条目'0'重复
答案 2 :(得分:0)
如果将Spring JPA与Entity Manager结合使用,则可以在服务层顶部添加@Transactional。
@Transactional
public class MeetServiceImpl {