2

I am running into the exception below whenever I use an entity that I have defined.

org.hibernate.exception.SQLGrammarException: Invalid column name 'coordinator_sycs_coord_id'.
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

I will post below the entities involved and the query that Hibernate is generating. The context is two entities that have a many-to-many relationship in an association table. I find interesting that the query that Hibernate is generating is changing the column name even when I have it right in my annotations. See below:

@Entity
@Table(name = "sycs_coord")
public class SycsCoordinator {

    @Id
    @GeneratedValue
    @Column(name = "sycs_coord_id")
    Long id;

    @OneToMany(mappedBy = "club", fetch = FetchType.LAZY)
    Set<SycsCoordinatorClub> clubs;

    //Standard setters and getters below
}

@Entity
@Table(name = "sycs_coord_clb")
@IdClass(SycsCoordinatorClubPk.class)
public class SycsCoordinatorClub {

    @Id
    @Column(name = "sycs_coord_id")
    Long sycs_coord_id;

    @Id
    @Column(name = "clb_id")
    String clb_id;

     @ManyToOne
    @PrimaryKeyJoinColumn(name = "sycs_coord_id", referencedColumnName="sycs_coord_id")
    SycsCoordinator coordinator;

    @ManyToOne
    @PrimaryKeyJoinColumn(name = "clb_id", referencedColumnName = "Clb_id")
    Club club;

}

I am not including the classes Club and SycsCoordinatorClubPk for now because they seem irrelevant to the problem. The query that Hibernate is generating some times is:

select
    clubs0_.club_Clb_Id as club4_0_3_,
    clubs0_.clb_id_fk as clb1_3_,
    clubs0_.sycs_coord_id as sycs2_3_,
    clubs0_.clb_id_fk as clb1_2_2_,
    clubs0_.sycs_coord_id as sycs2_2_2_,
    clubs0_.club_Clb_Id as club4_2_2_,
    clubs0_.coordinator_sycs_coord_id as coordina5_2_2_,
    clubs0_.lst_updt_dt as lst3_2_2_,
    clubs0_.sycs_coord_secur_grp_cd as sycs6_2_2_,
    sycscoordi1_.sycs_coord_id as sycs1_0_0_,
    sycscoordi2_.sycs_coord_secur_level_id as sycs4_3_1_ 
from
    sycs_coord_clb clubs0_ 
left outer join
    sycs_coord sycscoordi1_ 
        on clubs0_.coordinator_sycs_coord_id=sycscoordi1_.sycs_coord_id 
where
    clubs0_.club_Clb_Id=?

Notice that sometimes the column name coordinator_sycs_coord_id appears in the query, even when there is no such name in any of the annotations. Why is this?

1 Answer 1

2

You are mis-using the @PrimaryKeyJoinColumn annotation, hence the strange results:

It is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity.

http://docs.oracle.com/javaee/5/api/javax/persistence/PrimaryKeyJoinColumn.html

You should probably be using the @JoinColumn instead:

@JoinColumn(name = "sycs_coord_id", referencedColumnName = "sycs_coord_id")
Sign up to request clarification or add additional context in comments.

2 Comments

I will test the answer an accept as soon as I am able to fix it. Thanks a lot.
It worked. I had to also add updatable = false, insertable = false. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.