0

I am using JPA and hibernate as a JPA provider to connect to my Database. I have got a table in the database with name Candidates and here is my JPA Entity.

 @Entity
 public class Candidate {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    public long cid;

    public String name;

    @ManyToOne
    @JoinColumn(name="userCharacter")
    public UserCharacter userCharacter;
}

Here is one of the random JPA query from my code.

String queryStr = "SELECT new models.CandidateReport(c.cid, c.name, uc.rating) "
                + "FROM Candidate c JOIN c.userCharacter uc";

Then hibernate has generated sql as

select candidate0_.cid as col_0_0_, candidate0_.name as col_1_0_, usercharac1_.rating as col_2_0_ 
from Candidate candidate0_ 
join UserCharacter usercharac1_
on candidate0_.userCharacter=usercharac1_.uid

Meaning the Entity Candidate is not queried as Candidates(Plural). After explicitly specifying the @Table(name="Candidates") the problem is resolved. Doesn't JPA or Hibernate automatically query for plural names, example if Entity name is User, doesn't it look for users table in the database, I guess it will and I am missing some configuration.

srk
  • 4,857
  • 12
  • 65
  • 109

1 Answers1

3

Since the default table name in JPA for a class is the same as the class name (Candidate) then you MUST put @Table if you want to map it to a different table name. As per all JPA tutorials would say

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29