title: Why am I obtaining this "Repeated column in mapping for entity" into an Hibernate entity? What is the best way to handle this situation?
body: <p>I am not so into Hibernate and I have the following problem\doubt.</p>
  
  <p>I have this situation:</p>
  
  <p>1) I have this <strong>Room</strong> entity class that represent a room of an accomodation:</p>
  
  <pre><code>@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&lt;RoomMedia&gt; 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
      ........................................................................
      ........................................................................
  }
  </code></pre>
  
  <p>2) Then I have this <strong>RoomMedia</strong> entity class that represent a photo related to a specific <strong>Room</strong> entity:</p>
  
  <pre><code>@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
      ........................................................................
      ........................................................................
  
  }
  </code></pre>
  
  <p>As you can see in the previous code these 2 entity classes are linked togheter in this way:</p>
  
  <p>A <strong>Room</strong> instance represent a specific room and contains the list of its photos, in this way:</p>
  
  <pre><code>@OneToMany(mappedBy = "room")
  private List&lt;RoomMedia&gt; roomMediaList;
  </code></pre>
  
  <p>that means that a single room is linked to many <strong>RoomMedia</strong> instances. The mappedBy = "room" means that to do the link between <strong>Room</strong> and <strong>RoomMedia</strong> use the <strong>room</strong> field of the <strong>RoomMedia</strong> entity (that represent the specific room of a <strong>RoomMedia</strong> instance).</p>
  
  <p>Then in the <strong>RoomMedia</strong> entity class I have this link:</p>
  
  <pre><code>@ManyToOne
  @JoinColumn(name = "id_room", nullable = false)    
  private Room room;
  </code></pre>
  
  <p>Where means that many <strong>RoomMedia</strong> instances are related to a single <strong>Room</strong> intsance. The Join operation is done using the <strong>id_room</strong> field on the <strong>room_media</strong> table (it is done at table level and not a entity level).</p>
  
  <p>Correct if all this reasoning have something wrong.</p>
  
  <p>My problem is that at the application startup I am obtaining this error message:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Why Hibernate can't handle both? What is the problem? How can I fix it?</p>
  
  <p>I think that maybe it could depend by the fact that in the <strong>RoomMedia</strong> instance I still have the <strong>id_room</strong> field that is no longer needed because in this entity I also have put the:</p>
  
  <pre><code>@ManyToOne
  @JoinColumn(name = "id_room", nullable = false)    
  private Room room;
  </code></pre>
  
  <p>that itself contains this id.</p>
  
  <p>So I think that removing this field:</p>
  
  <pre><code>@Column(name = "id_room")
  private Long idRoom;
  </code></pre>
  
  <p>I will solve my problem, is it?</p>
  
  <p>But now I could will have another problem: into my DAO I could have some complex query that involves the <strong>idRoom</strong>. So, for convenience, I wold have both these <strong>private Room room;</strong> and <strong>private Long idRoom;</strong> field into my <strong>RoomMedia</strong> entity class.</p>
  
  <p>How can I mantain both avoiding this issue?</p>