我不是很喜欢Hibernate而且我有以下问题\怀疑。
我遇到这种情况:
1)我有这个 Room 实体类,代表住宿的房间:
@Entity
@Table(name = "room")
public class Room implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "id_accomodation_fk", nullable = false)
private Accomodation accomodation;
@ManyToOne
@JoinColumn(name = "id_room_tipology_fk", nullable = false)
private RoomTipology roomTipology;
@OneToMany(mappedBy = "room")
private List<RoomMedia> roomMediaList;
@Column(name = "room_number")
private String number;
@Column(name = "room_name")
private String name;
@Column(name = "room_description")
private String description;
@Column(name = "is_enabled")
private Boolean isEnabled;
........................................................................
........................................................................
GETTER AND SETTER METHODS
........................................................................
........................................................................
}
2)然后我有这个 RoomMedia 实体类,它代表与特定房间实体相关的照片:
@Entity
@Table(name = "room_media")
public class RoomMedia {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "id_room")
private Long idRoom;
@ManyToOne
@JoinColumn(name = "id_room", nullable = false) // da rinominare anche sul DB in room_fk
private Room room;
@Lob
@Column(name = "media")
private byte[] media;
private String description;
........................................................................
........................................................................
GETTER AND SETTER METHODS
........................................................................
........................................................................
}
正如您在前面的代码中所看到的,这两个实体类以这种方式链接到togheter:
会议室实例代表特定会议室,并以这种方式包含其照片列表:
@OneToMany(mappedBy = "room")
private List<RoomMedia> roomMediaList;
这意味着单个会议室与许多 RoomMedia 实例相关联。 mappedBy =&#34;房间&#34;表示要在房间和 RoomMedia 之间建立链接,请使用 RoomMedia 实体的会议室字段(代表 RoomMedia 实例的特定房间。
然后在 RoomMedia 实体类中,我有这个链接:
@ManyToOne
@JoinColumn(name = "id_room", nullable = false)
private Room room;
表示许多 RoomMedia 实例与单个 Room intsance相关。加入操作是使用 room_media 表上的 id_room 字段完成的(它是在表级而非实体级别完成的。)
如果所有这些推理都有问题,请更正。
我的问题是,在应用程序启动时,我收到此错误消息:
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
... 39 more
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.betrivius.domain.RoomMedia column: id_room (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:709)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:731)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:753)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:506)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
... 48 more
为什么Hibernate无法同时处理这两个问题?问题是什么?我该如何解决?
我认为也许这可能取决于在 RoomMedia 实例中我仍然不再需要 id_room 字段,因为在此实体中我也有把:
@ManyToOne
@JoinColumn(name = "id_room", nullable = false)
private Room room;
本身包含此ID。
所以我认为删除这个字段:
@Column(name = "id_room")
private Long idRoom;
我会解决我的问题,是吗?
但现在我可能会遇到另一个问题:在我的DAO中,我可能会遇到一些涉及 idRoom 的复杂查询。因此,为了方便起见,我将这些私人房间; 和私人Long idRoom; 字段放入我的 RoomMedia 实体类。
我怎样才能避免这个问题呢?
答案 0 :(得分:2)
您绝对应该从映射中删除此列
@Column(name = "id_room")
private Long idRoom;
和Hibernate告诉你一样。
但现在我可能会遇到另一个问题:在我的DAO中,我可能会遇到一些涉及idRoom的复杂查询。因此,为了方便起见,我将拥有这两间私人房间;和私人Long idRoom;字段进入我的RoomMedia实体类。
正确的映射是
@ManyToOne
@JoinColumn(name = "id_room", nullable = false)
private Room room;
和它可以为您提供所需的一切
查询示例
Query query1 = em.createQuery("select rm from RoomMedia rm where rm.room.id = :id");
Query query2 = em.createQuery("select rm from RoomMedia rm join rm.room r where r.id = :id");