简体   繁体   中英

Set Hibernate access strategy globally?

According to the Hibernate documentation , the placement of the @Id annotation determines how Hibernate will access the entity (field or accessors)

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access. Place on the identifier getter, Hibernate will use property-based access.

Is it possible to define this globally via a property (To avoid having to place a @Access(AccessType.FIELD) on each entity or embeddable)?

I found this related question , but that is for Spring Boot specifically.

You can create a file called orm.xml and put it to the classpath in directory META-INF .

In that file you can set default values. For example the access type:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings>
    <persistence-unit-metadata>
            <access>PROPERTY</access>
    </persistence-unit-metadata>
</entity-mappings>

You can find the XML Schema here: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/resources/org/hibernate/jpa/orm_2_2.xsd

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