所以,亲爱的同志们,再次问好。我遇到了弹簧靴jpa的问题。 我无法加入专栏。我正在使用JpaRepository。 所以这里的代码: application.properties:
spring.data.jpa.repositories.enabled=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/project_db?user=root&password=root
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
角色:
public enum Roles implements Serializable {
ROLE_USER,
ROLE_ADMIN
}
用户:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long Id;
@Basic
@Column(name = "username")
private String username;
@Basic
@Column(name = "password")
private String password;
@Basic
@Column(name = "fullname")
private String fullName;
@ElementCollection(targetClass = Roles.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<Roles> roles;
这里Hibernate尝试保存实体:
Hibernate: insert into users (fullname, password, username) values (?, ?, ?)
Hibernate: insert into user_roles (user_id, roles) values (?, ?)
异常跟踪追踪:
`javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statementce.PersistenceException:
Caused by: java.sql.SQLException: ConnectionImple.registerDatabase - ARJUNA017017: enlist of resource failed
com.mysql.jdbc.jdbc2.optional.MysqlXAException: No operations allowed after connection closed.
java.sql.SQLException: ConnectionImple.registerDatabase - ARJUNA017017: enlist of resource failed
我认为理由是这样的:
com.mysql.jdbc.jdbc2.optional.MysqlXAException: No operations allowed after connection closed.