简体   繁体   中英

How to use @AttributeOverrides when used method access to property?

I have an Embeddable class with method access to properties. It works fine until there two or more inclusions in one other class where I have to use @AttributeOverrides.

At this case I receive the following error from Hibernate:

Repeated column in mapping for entity: ... column: grn_date (should be mapped with insert="false" update="false")

It seems Hibernate for some reasons doesn't understand method access. If I use common property-access all works fine, but I need current one to get Calendar date from XMLGregorianCalendar field.

Embeddable class:

@Embeddable
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "...")
public class GrnType {

    @XmlAttribute(name = "...")
    @Transient
    protected String grn;

    @XmlAttribute(name = "...", required = true)
    @Transient
    protected XMLGregorianCalendar dateOfEntry;

    @Transient
    private Calendar calendarDate;


    @Access(AccessType.PROPERTY)
    @Column(name = "grn")
    public String getGrn() {
        return grn;
    }

    @Access(AccessType.PROPERTY)
    @Column(name = "grn_date")
    private Calendar getCalendarDate() {
         if(dateOfEntry!=null) {
             return new GregorianCalendar(dateOfEntry.getYear(), dateOfEntry.getMonth(), dateOfEntry.getDay());
         }else {
             return null;
         }
    }

    //   ...setters here

}

Class where embedded:

@Entity
@Table(name="RealEstateDFl")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {...})
public class GrnEntity {

    //...
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "grn", column = @Column(name = "grn_main")),
            @AttributeOverride(name = "grn_date", column = @Column(name = "grn_date_main"))
    })
    protected GrnType grnType;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "grn", column = @Column(name = "grn_sec")),
            @AttributeOverride(name = "grn_date", column = @Column(name = "grn_date_sec"))
    })
    protected GrnType grnTypeSecondary;

    //...
}

Could anyone advice how to override attributes properly at this case? Or what might be the solution?

I have found the solution. It was pretty tricky for me.

At the class where embedded it needs to set not the name of the column but the name of the variable , in my case it is calendarDate .

So here is a working example:

@Entity
@Table(name="RealEstateDFl")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {...})
public class GrnEntity {

    //...
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "grn", column = @Column(name = "grn_main")),
            @AttributeOverride(name = "calendarDate", column = @Column(name = "grn_date_main"))
    })
    protected GrnType grnType;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "grn", column = @Column(name = "grn_sec")),
            @AttributeOverride(name = "calendarDate", column = @Column(name = "grn_date_sec"))
    })
    protected GrnType grnTypeSecondary;

    //...
}

Also getters and setters in Embeddable class should exactly match the property names.

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