简体   繁体   中英

Can I mix hbm.xml mapping and jpa annotation mapping for a single entity in hibernate?

I have some java model classes that are currently mapped to database tables using JPA annotations. one of them is like this:

@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

}

I've got a task to map these java model to a secondary schema with the same tables. So I am thinking to move the mapping to hbm.xml files and then incorporate the new mapping for new schema. I am wondering if I can move just the table and entity names to the hbm.xml file and keep the fileds mapping in the java class as they are. Something like this:

<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>

Is it a good implementation for this problem or is there any better way to do this? I just want to avoid converting all the jpa mappings to hbm.xml mapping.

This is the typical usage of mixing xml and annotation ... when you need to override some hard coded annotations for a certain data store ... yet there are some rules (rules for overriding) that you need to take in consideration ... as per "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.

I recommend that you read the first 10 pages of chapter 13 in this book to get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM