我想将运行时生成的实体UUID复制到另一个字段。 实体ID是通过下面描述的代码生成的:
package eclipselink.example;
public class UUIDSequence extends Sequence implements SessionCustomizer {
public UUIDSequence() {
super();
}
public UUIDSequence(String name) {
super(name);
}
@Override
public Object getGeneratedValue(Accessor accessor,
AbstractSession writeSession, String seqName) {
return UUID.randomUUID().toString().toUpperCase();
}
...
public void customize(Session session) throws Exception {
UUIDSequence sequence = new UUIDSequence("system-uuid");
session.getLogin().addSequence(sequence);
}
}
Persitence.xml:
property name="eclipselink.session.customizer" value="eclipselink.example.UUIDSequence"
实体:
public abstract class MyEntity{
private String id;
private String idCopy;
@Id
@Basic(optional = false)
@GeneratedValue(generator="system-uuid")
@XmlElement(name = "ID")
public String getId() {
return id;
}
}
我如何指示JPA(Eclipse-link)将运行时生成的UUID复制到idCopy
字段?
答案 0 :(得分:0)
我不是100%肯定这会起作用(我不知道EclipseLink是否调用了setter或直接指定了字段),但试试看:
public abstract class MyEntity{
private String id;
private String idCopy;
@Id
@Basic(optional = false)
@GeneratedValue(generator="system-uuid")
@XmlElement(name = "ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
this.idCopy = id;
// or
// this.setIdCopy(id);
}
}