我可以在hibernate中为单个实体混合使用hbm.xml映射和jpa注释映射吗?

时间:2017-10-03 15:07:13

标签: java hibernate jpa

我有一些当前使用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文件并保持fileds映射到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映射。

1 个答案:

答案 0 :(得分:1)

这是混合xml和注释的典型用法...当你需要覆盖某个数据存储的某些硬编码注释时......但是你需要考虑一些规则(覆盖规则) ......按照&#34; Pro JPA 2&#34; :

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页以获得想法。