JPA序列化

发布于 2024-12-05 01:19:45 字数 2055 浏览 4 评论 0原文

我正在 MySQL 5 上使用 JPA 2.0-EclipseLink-Hibernate 3。我的类结构如下所示(我对其进行了简化)

public class Status implements Serialization {
    private String name = null;

    public Status(string n) {
        this.name = n;
    }

    @Override
    public String toString() {
        return this.name;
    }
}

@Entity
@Table(name="ACCOUNT")
public class Account {

    public static final Status ACTIVE = new Status("AC");
    public static final Status DISABLE = new Status("DI");

    private Status status = null;
    private long id = 0;

    public Account(long id, Status s) {
        this.id = id;
        this.status = s;
    }

    @Id
    @Column(name="id")
    public long getId() {
        return this.id;
    }

    public setId(long id) {
        this.id = id;
    }

    @Column(name="status")
    public Status getStatus() {
        return this.status;
    }

    public setStatus(Status s) {
        this.status = s;
    }
}

现在,我的问题是,当我使用 EntityManager.persist 插入新帐户时,程序会引发异常:

java.sql.SQLException:错误的字符串值:'\xAC\xED\x00\x05sr...' 对于第 1 行的“状态”列

,在我手动将新帐户添加到数据库中,然后选择它后,程序显示:

异常描述:无法从字节数组反序列化对象。 内部异常:java.io.EOFException 映射: org.eclipse.persistence.mappings.DirectToFieldMapping[状态-->ACCOUNT.status] 描述: RelationalDescriptor(net.serenco.serenekids.model.bean.account.Account --> [数据库表(帐户)])

请告诉我如何控制将哪些值写入数据库,顺便请帮助我修复这些错误。提前致谢!

---------------- 编辑----------------

我自己设法找到了解决方案。这是:

@Embeddable
public class Status implements Serialization {
    ...

    protected String getName() {
        return name;
    }

    protected void setName(String name) {
        this.name = name;
    }
}

@Entity
@Table(name="ACCOUNT")
public class Account {
    ...
    @Embedded
    @AttributeOverrides( {
            @AttributeOverride(name="name", column = @Column(name="status") )
    } )
    public Status getStatus() {
        return this.status;
    }
    ...
}

I'm working with JPA 2.0-EclipseLink-Hibernate 3 on MySQL 5. My classes structure is like below (I simplified it)

public class Status implements Serialization {
    private String name = null;

    public Status(string n) {
        this.name = n;
    }

    @Override
    public String toString() {
        return this.name;
    }
}

@Entity
@Table(name="ACCOUNT")
public class Account {

    public static final Status ACTIVE = new Status("AC");
    public static final Status DISABLE = new Status("DI");

    private Status status = null;
    private long id = 0;

    public Account(long id, Status s) {
        this.id = id;
        this.status = s;
    }

    @Id
    @Column(name="id")
    public long getId() {
        return this.id;
    }

    public setId(long id) {
        this.id = id;
    }

    @Column(name="status")
    public Status getStatus() {
        return this.status;
    }

    public setStatus(Status s) {
        this.status = s;
    }
}

Now, my question is, when I insert a new Account using EntityManager.persist, the program throws an exception:

java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05sr...'
for column 'status' at row 1

And after I added a new Account manually into the database, then I select it, the program say:

Exception Description: Could not deserialize object from byte array.
Internal Exception: java.io.EOFException Mapping:
org.eclipse.persistence.mappings.DirectToFieldMapping[status-->ACCOUNT.status]
Descriptor:
RelationalDescriptor(net.serenco.serenekids.model.bean.account.Account
--> [DatabaseTable(ACCOUNT)])

Please tell me how to control what value will be written to DB, and by the way, please help me fix these errors. Thanks in advance!

---------------- EDIT ----------------

I have managed to find out the solution myself. It is:

@Embeddable
public class Status implements Serialization {
    ...

    protected String getName() {
        return name;
    }

    protected void setName(String name) {
        this.name = name;
    }
}

@Entity
@Table(name="ACCOUNT")
public class Account {
    ...
    @Embedded
    @AttributeOverrides( {
            @AttributeOverride(name="name", column = @Column(name="status") )
    } )
    public Status getStatus() {
        return this.status;
    }
    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文