【问题标题】:Can I mix hbm.xml mapping and jpa annotation mapping for a single entity in hibernate?我可以在休眠中为单个实体混合 hbm.xml 映射和 jpa 注释映射吗?
【发布时间】:2017-10-04 00:23:53
【问题描述】:

我有一些当前使用 JPA 注释映射到数据库表的 java 模型类。其中一个是这样的:

@Table(name = "RECOG_APPLICATION_STAGING")
@SequenceGenerator(name = "seqRecogApplicationStaging", allocationSize = 0, sequenceName = "SEQ_RECOG_APPLICATION_STAGING")
@Entity
public class RecogApplicationStaging extends AuditedEntity implements Serializable {

@Id
@Column(name = "APPLICATION_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqRecogApplicationStaging")
private Long id;


@OneToOne(mappedBy = "applicationStaging", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private RecogPropertyStaging recogPropertyStaging;

@OneToOne(mappedBy = "applicationStaging", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private ContactInformation contactInformation;

@OneToOne(mappedBy = "applicationStaging", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private RecogEligibilityStaging recogEligibilityStaging;

@OneToOne(mappedBy = "applicationStaging", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private RecogSubmitApplStaging recogSubmitApplStaging;

@OneToMany(mappedBy = "applicationStaging", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Sort(type = SortType.NATURAL)
private SortedSet<GenerateSignature> generateSignature = new TreeSet<>();

@Column(name = "READY_FOR_DOWNLOAD_YN")
private boolean readyForDownloadYn;

@Column(name = "RECOG_APPL_PROGRESS_CODE")
private String recogApplProgressCode;

@Column(name = "DOWNLOAD_TRACKING_NUMBER")
private String downloadTrackingNumber;

@Column(name = "PROPERTY_ID")
private Long propertyId;

//getters and setters

}

我的任务是将这些 java 模型映射到具有相同表的辅助模式。所以我正在考虑将映射移动到 hbm.xml 文件,然后为新模式合并新映射。我想知道是否可以仅将表和实体名称移动到 hbm.xml 文件中,并将文件映射保留在 java 类中。像这样的:

<hibernate-mapping package="....dao.model.recognition" default-access="field">

<class
    name="....dao.model.recognition.RecogApplicationStaging"
    table="RECOG_APPLICATION_STAGING"
    entity-name="primaryRecogApplicationStaging">
    <id name="id" column="APPLICATION_ID">
        <generator class="native">
            <param name="sequence_name">SEQ_RECOG_APPLICATION_STAGING</param>
        </generator>
    </id>

</class>

<class
        name="....dao.model.recognition.RecogApplicationStaging"
        table="RECOG_APPLICATION_STAGING"
        entity-name="secondaryRecogApplicationStaging">
    <id name="id" column="APPLICATION_ID">
        <generator class="native">
            <param name="sequence_name">SEQ_RECOG_APPLICATION_STAGING</param>
        </generator>
    </id>
</class>

这是解决这个问题的好方法还是有更好的方法来解决这个问题?我只是想避免将所有 jpa 映射转换为 hbm.xml 映射。

【问题讨论】:

  • JPA 使用 orm.xml,而不是某些 Hibernate 特定的遗物。

标签: java hibernate jpa


【解决方案1】:

这是混合 xml 和注释的典型用法......当您需要为某个数据存储覆盖一些硬编码的注释时......但是您需要考虑一些规则(覆盖规则) ...根据“Pro JPA 2”:

The following algorithm can be considered as the simplified logic for obtaining the metadata for the persistence unit: 1. Process the annotations. The set of entities, mapped superclasses, and embedded objects (we’ll call this set E) is discovered by looking for the @Entity, @MappedSuperclass, and @Embeddable annotations. The class and method annotations in all the classes in set E are processed, and the resulting metadata is stored in set C. Any missing metadata that was not explicitly specified in the annotations is left empty. 2. Add the classes defined in XML. Look for all the entities, mapped superclasses, and embedded objects that are defined in the mapping files and add them to E. If we find that one of the classes already exists in E, we apply the overriding rules for class-level metadata that we found in the mapping file. Add or adjust the class-level metadata in C according to the overriding rules. 3. Add the attribute mappings defined in XML. For each class in E, look at the fields or properties in the mapping file and try to add the method metadata to C. If the field or property already exists there, apply the overriding rules for attribute-level mapping metadata. 4. Apply defaults. Determine all default values according to the scoping rules and where defaults might have been defined (see the following for description of default rules). The classes, attribute mappings, and other settings that have not yet been filled in are assigned values and put in C.

我建议您阅读本书第 13 章的前 10 页以了解这个想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2012-09-05
    • 2019-12-03
    相关资源
    最近更新 更多