0

我想将运行时生成的实体 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);
    }
}

持久性.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字段中?

4

1 回答 1

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);
    }
}
于 2012-04-23T15:17:07.280 回答