Hibernate @AttributeOverride 适用于实体但不适用于 属性
Hibernate @AttributeOverride works on Entity but not on property
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 Book
秒(book
和 ebook
)
@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
时(如评论中所示 - 它有效),但在 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 - 它起作用了。
很可能你得到这个错误是因为你试图混淆 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
}
P.S。但是,如果您需要,可以使用 @Access
注释 override 默认访问策略。
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 Book
秒(book
和 ebook
)
@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
时(如评论中所示 - 它有效),但在 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 - 它起作用了。
很可能你得到这个错误是因为你试图混淆 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
}
P.S。但是,如果您需要,可以使用 @Access
注释 override 默认访问策略。