我使用EclipseLink + PostgreSQL
我需要创建具有UUID(非主键)字段的空值的实体。
这是我的实体,字段 idUser 在某些情况下必须为null:
@Entity
@Table(schema = "bd00", name = "t_bd00_messages")
public class TBd00Messages implements Serializable {
@Id
@Column(name = "id")
@Getter @Setter
private UUID id;
@Getter @Setter
private String cdoc;
@Getter @Setter
private String receiver;
@Getter @Setter
private String message;
@Column(name = "status")
@Getter @Setter
private Integer statusCode;
@Temporal(TemporalType.TIMESTAMP)
@Getter @Setter
private Date tstmp;
@Column(name = "d_send")
@Temporal(TemporalType.TIMESTAMP)
@Getter @Setter
private Date dSend;
@Column(name = "id_user")
@Getter @Setter
private UUID idUser;
@Getter @Setter
private String subject;
当我持久化对象时, idUser = null - a有例外:
Internal Exception: org.postgresql.util.PSQLException: ERROR: column "id_user" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 125
Error Code: 0
Call: INSERT INTO bd00.t_bd00_messages (id, CDOC, d_send, id_user, MESSAGE, RECEIVER, status, SUBJECT, TSTMP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [********, *******, 2016-07-21 14:35:40.858, null, ......
答案 0 :(得分:1)
对于基本映射,EclipseLink将传入null值并让驱动程序转换,这显然不能为UUID null做。
要解决这个问题,您需要告诉EclipseLink如何为驱动程序指定类型,如本文https://stackoverflow.com/a/15084881/496099中所示。 关键部分是打电话
Field.setSqlType(Types.OTHER);
Field.setTypeName("uuid");
Field.setColumnDefinition("UUID");
在映射中使用的字段。
答案 1 :(得分:0)
这将允许您将空值插入指定的列。
@Column(name = "id_user", nullable = true)