Jpa 对简单连接长异常的奇怪值 table
Jpa strange value to long exception on simple join table
我有 2 个实体项目和用户:
@Entity
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column(unique = true)
private String name;
@Column
private String description;
}
和
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column(unique = true)
private String name;
}
我想创建一个加入 table 实体(分配给用户的项目)并想在预订实体中使用它。我使用了@IdClass 方法(我得到了与可嵌入相同的结果):
@Entity
@IdClass(UserProjectKey.class)
public class UserProjects implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@ManyToOne
private User user;
@Id
@ManyToOne
private Project project;
public UserProjects() {
}
public UserProjects(final User user, final Project project){
this.user = user;
this.project = project;
}
}
public class UserProjectKey implements Serializable {
private static final long serialVersionUID = 1L;
private Long user;
private Long project;
public UserProjectKey() {
}
public UserProjectKey(final long userId, final long projectId){
user = userId;
project = projectId;
}
}
在预订中使用:
@Entity
public class Booking implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column
@NotNull
private UserProjects userProject;
@Column
@NotNull
private Timestamp start;
@Column
@NotNull
private Timestamp end;
}
我省略了所有的 getter 和 setter。问题出在持久预订上,我得到一个奇怪的异常:
Value too long for column "USERPROJECT BINARY(255) NOT NULL": "X'aced00057372002d636f6d2e70726f64796e612e7061632e74696d747261636b65722e6d6f64656c2e5573657250726f6a6563747300000000000000010200... (572)"; SQL statement:
insert into Booking (end, start, userProject, version, id) values (?, ?, ?, ?, ?) [22001-173]
因此出于某种原因,jpa 尝试将 UserProject 存储为二进制文件而不是 UserProject table 的外键。那么如何修复它,如何为这个用例正确设计 jpa 类?
您与 UserProjects
的关系未定义为外键。您需要使用 @ManyToOne
(我假设许多预订可能涉及一个 UserProjects
实例?)以及 @JoinColumn
(而不是您的 @Column
)才能触发一个外键。我相信您看到的是试图插入的序列化值。
我有 2 个实体项目和用户:
@Entity
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column(unique = true)
private String name;
@Column
private String description;
}
和
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column(unique = true)
private String name;
}
我想创建一个加入 table 实体(分配给用户的项目)并想在预订实体中使用它。我使用了@IdClass 方法(我得到了与可嵌入相同的结果):
@Entity
@IdClass(UserProjectKey.class)
public class UserProjects implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@ManyToOne
private User user;
@Id
@ManyToOne
private Project project;
public UserProjects() {
}
public UserProjects(final User user, final Project project){
this.user = user;
this.project = project;
}
}
public class UserProjectKey implements Serializable {
private static final long serialVersionUID = 1L;
private Long user;
private Long project;
public UserProjectKey() {
}
public UserProjectKey(final long userId, final long projectId){
user = userId;
project = projectId;
}
}
在预订中使用:
@Entity
public class Booking implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column
@NotNull
private UserProjects userProject;
@Column
@NotNull
private Timestamp start;
@Column
@NotNull
private Timestamp end;
}
我省略了所有的 getter 和 setter。问题出在持久预订上,我得到一个奇怪的异常:
Value too long for column "USERPROJECT BINARY(255) NOT NULL": "X'aced00057372002d636f6d2e70726f64796e612e7061632e74696d747261636b65722e6d6f64656c2e5573657250726f6a6563747300000000000000010200... (572)"; SQL statement:
insert into Booking (end, start, userProject, version, id) values (?, ?, ?, ?, ?) [22001-173]
因此出于某种原因,jpa 尝试将 UserProject 存储为二进制文件而不是 UserProject table 的外键。那么如何修复它,如何为这个用例正确设计 jpa 类?
您与 UserProjects
的关系未定义为外键。您需要使用 @ManyToOne
(我假设许多预订可能涉及一个 UserProjects
实例?)以及 @JoinColumn
(而不是您的 @Column
)才能触发一个外键。我相信您看到的是试图插入的序列化值。