【问题标题】:Hibernate @AttributeOverride works on Entity but not on propertyHibernate @AttributeOverride 适用于实体但不适用于属性
【发布时间】:2021-06-07 15:27:03
【问题描述】:

Hibernate 版本 5.4.29(独立测试)或 5.4.28(使用 SpringBoot 2.4.3 测试)

以下型号: 可嵌入Book

import javax.persistence.Embeddable;
    
@Embeddable
public class Book {
    public Book() { }
        
    private String name = "";
    private String author = "";
    
    // ctors, getters, setters removed for brevity 
        
}

事件嵌入 2 Books(bookebook

@Entity
@Table( name = "EVENTS" )
/*@AttributeOverrides({
    @AttributeOverride(name = "book.name", column = @Column(name = "book_title")), 
    @AttributeOverride(name = "book.author", column = @Column(name = "book_author")),
    @AttributeOverride(name = "ebook.name", column = @Column(name = "ebook_title")), 
    @AttributeOverride(name = "ebook.author", column = @Column(name = "ebook_author"))
})*/
public class Event {
   private Long id;
   private String title;
   private Date date;
        
   @Embedded
   @AttributeOverrides({
      @AttributeOverride(name = "name", column = @Column(name = "book_title")), 
      @AttributeOverride(name = "author", column = @Column(name = "book_author")),
   })
   private Book book = new Book();
        
   @Embedded
   @AttributeOverrides({
      @AttributeOverride(name = "name", column = @Column(name = "ebook_title")), 
      @AttributeOverride(name = "author", column = @Column(name = "ebook_author")),
   })
   private Book ebook = new Book();
    
   // ctors, getters, setters removed for brevity 
}

当尝试在实体上配置 @AttributeOverride 时(如 cmets 所示 - 它可以工作),但是当在 Book 类型的具体 @Embedded 属性上时它不起作用。 收到以下错误:

    Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.example.demo.Event.Event column: author (should be mapped with insert="false" update="false")
        at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:876) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:354) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:298) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]

持久化实体

Event e = new Event( "A booked event", new Date() );
e.getBook().setAuthor("Jora K Jr");
e.getBook().setName("Book Name");
entityManager.persist( e3 );

我做错了什么?在 Hibernate 4 中,AFAIR - 它起作用了。

【问题讨论】:

    标签: java hibernate jpa orm hibernate-mapping


    【解决方案1】:

    您很可能会收到此错误,因为您尝试混淆 access strategies 和休眠只是忽略您的 @AttributeOverride 注释。默认情况下,@Id 注解的放置给出了默认的访问策略。

    因此,请尝试以这种方式更正您的实体映射:

    @Entity
    @Table( name = "EVENTS" )
    public class Event {
    
        @Id
        private Long id;
    
        // ...
        
        @Embedded
        @AttributeOverrides({
            @AttributeOverride(name = "name", column = @Column(name = "book_title")), 
            @AttributeOverride(name = "author", column = @Column(name = "book_author")),
        })
        private Book book = new Book();
        
        @Embedded
        @AttributeOverrides({
            @AttributeOverride(name = "name", column = @Column(name = "ebook_title")), 
            @AttributeOverride(name = "author", column = @Column(name = "ebook_author")),
        })
        private Book ebook = new Book();
    
        //ctors, getters, setters removed for brevity 
    
    }
    

    附:但是,如果您需要,您可以使用@Access 注解override 默认访问策略。

    【讨论】:

    • 明白了!按照您的建议进行了修复,并且可以正常工作!谢谢指点我访问策略:)
    猜你喜欢
    • 2017-06-19
    • 2012-04-14
    • 2016-11-25
    • 1970-01-01
    • 2013-01-24
    • 2018-05-24
    • 2020-09-13
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多