0

I'm working on a legacy database for which I have generated the following entity classes:

Business entity

@Entity
public class Business { ... }

Frontuser entity

@Entity
public class Frontuser { ... }

Review entity

@Entity
@Table(name="review")
public class Review {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false)
    private int businesshoteltravelId;

    @Column(name = "frontuser_id", nullable=false)
    private int frontuser_id;

    @Column(name = "business_id", nullable=false)
    private int business_id;

    @ManyToOne
    @JoinColumn(name="frontuserId", nullable=false)
    private Frontuser frontuser;

    @ManyToOne
    @JoinColumn(name="businessId", nullable=false)
    private Business business;
}

As seen, the columns frontuser_id and business_id are foreign key columns to other tables. Columns frontuserId and businessId are an abomination (from a naming convention perspective).

It is worth noting that the review table actually contains the four columns frontuser_id, business_id, frontuserId and businessId.

With these entity declarations, my application fails to start with the following exception:

org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'entityManagerFactory'
    Table [review] contains physical column name [business_id] referred to by multiple physical column names: [businessId], [business_id]

I am not sure why this error occurs, given that I have explicitly named table columns in the entity class declaration.

How I can map these attributes with JPA?

manish
  • 19,695
  • 5
  • 67
  • 91
Tlabs
  • 105
  • 8
  • 1
    What's wrong with @@Column, @@OneToOne, @@JoinColumn etc? – Argb32 Dec 13 '16 at 20:19
  • I obtain this error: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Table [review] contains physical column name [business_id] referred to by multiple physical column names: [businessId], [business_id] – Tlabs Dec 14 '16 at 04:52
  • my code: `code` @Column(name = "frontuser_id", nullable=false) private int frontuser_id; @Column(name = "business_id", nullable=false) private int business_id; //bi-directional many-to-one association to Frontuser @ManyToOne @JoinColumn(name="frontuserId", nullable=false) private Frontuser frontuser; //bi-directional many-to-one association to Business @ManyToOne @JoinColumn(name="businessId", nullable=false) private Business business; – Tlabs Dec 14 '16 at 04:55
  • Please add the full entity code (class structures included) and the error you are seeing to the question. It is difficult to understand the problem from comments. – manish Dec 14 '16 at 06:07
  • It seems that you are using Hibernate as the JPA provider. If that is indeed the case, the root cause for the error you are seeing is the same as that for [this post](http://stackoverflow.com/questions/25283198/spring-boot-jpa-column-name-annotation-ignored). Basically, Hibernate translates `businessId` as `business_id`, hence the error. You will need to use a different naming strategy (probably `EJB3NamingStrategy`), as explained in the linked post to work around the error. – manish Dec 14 '16 at 12:56

0 Answers0