java - JPA 在持久化对象时丢失字段

标签 java mysql jpa eclipselink

首先,对这么长的帖子表示歉意,我已尽力将其删减。

我正在尝试使用 JPA 持久保存一个对象。虽然大多数对象字段都保存得很好,但有一个字段被完全跳过,但没有抛出任何错误。

简而言之,我正在尝试保留 ListedItemDetail 的实例。不幸的是,BidCollection 字段被完全忽略,并且数据库中没有它的踪迹。我不知道为什么..

我已经包含了相关来源。源是从第 3 方架构生成的。我使用 orm.xml 进行 JPA 映射,并且也包含了下面的 reavent 位。

public class ListedItemDetail  extends Item implements Serializable{
    protected BidCollection bids;
}

public class Item extends ExtensibleDataObject implements Serializable {
    protected int listingId;
}

public class BidCollection extends PagedCollectionOfBidte0R55Be implements
Serializable{
    private final static long serialVersionUID = 1L;
}

public class PagedCollectionOfBidte0R55Be implements Serializable {
    private final static long serialVersionUID = 1L;
    protected Integer totalCount;
    protected Integer page;
    protected Integer pageSize;
    protected InnerCollectionOfBidte0R55Be list;
}

public class InnerCollectionOfBidte0R55Be implements Serializable {
    private final static long serialVersionUID = 1L;
    protected List<Bid> bid;
}

public class Bid extends ExtensibleDataObject implements Serializable {
    private final static long serialVersionUID = 1L;
    protected String account;
    protected Boolean isByMobile;
    protected Boolean isByProxy;
    protected Timestamp bidDate;
    protected Boolean isBuyNow;
    protected Member bidder;
}

orm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0">
<entity class="nz.co.trademe.api.v1.ListedItemDetail">
    <attributes>
         <cascade>
             <cascade-persist/>
         </cascade>
        <embedded name="bids">
        </embedded>
    </attributes>
</entity>

<entity class="nz.co.trademe.api.v1.Item">
    <inheritance strategy="JOINED"/>
    <discriminator-column discriminator-type="STRING"/>
    <attributes>
        <id name="listingId">
        </id>
    </attributes>
</entity>


<embeddable class="nz.co.trademe.api.v1.PagedCollectionOfBidte0R55Be">
</embeddable>

<embeddable class="nz.co.trademe.api.v1.InnerCollectionOfBidte0R55Be">
    <attributes>
        <element-collection name="bid">
        </element-collection>
    </attributes>
</embeddable>

<embeddable class="nz.co.trademe.api.v1.Bid">
</embeddable>

<embeddable class="nz.co.trademe.api.v1.BidCollection">
</embeddable>
</entity-mappings>

最佳答案

该问题与 BidCollection 的可嵌入性和使用继承有关。 JPA 不支持嵌入继承。

从技术上讲,EclipseLink 确实支持嵌入的继承,但目前不支持通过 JPA 注释进行继承。

您可以删除继承,或尝试使用 DescriptorCustomizer 设置继承。

你的模型看起来也很复杂,你可能需要重新考虑一下。

关于java - JPA 在持久化对象时丢失字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8599916/

相关文章:

Spring数据投影和错误: "No aliases found in result tuple! Make sure your query defines aliases!"

java.lang.ClassNotFoundException 错误

java - 四个字节中的四个整数?

java - 使用 Spark 从 HDFS 到 Oracle BLOB 的 CSV 文件

php - "Insert into select from group by count on duplicate update"组合

mysql - MySQL FIND_IN_SET 或等效项可以使用索引吗?

java - 如何使用 Java 创建 Excel 表格?

php - 安全登录?有任何想法吗?任何方法都会很好

java - JPA+ Spring : Mapping a foreign key column to a ID based on the value received before persisting the entity

java - CrudRepository 保存方法不返回更新后的实体