简体   繁体   中英

Hibernate ManyToOne JOIN by non-PK column throws “Bad value for type int”

First of all let me make my scenario clear I am using hibernate 4.3 ,Postgresql 9.3 with spring MVC ,and I have reverse engineered classes using JBOSS tool.

Every time I select the data using HQL or session.get() I am getting (SQL queries are working fine) Caused by : org.postgresql.util.PSQLException: Bad value for type int : demoemailaddr@gmail.com

Entity classes,
Applicant.java

@Entity
@Table(name = "applicant", schema = "public", uniqueConstraints = { @UniqueConstraint(columnNames = "user_id"), @UniqueConstraint(columnNames = "email"), @UniqueConstraint(columnNames = "registration_id") })
public class Applicant implements java.io.Serializable
{
    @Id
    @Column(name = "application_id", unique = true, nullable = false, length = 7)
    private String applicationId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "email", unique = true, nullable = false)
    private Users usersByEmail;

     // setters and getters
}

and Users class which has @ManytOne relationship with above class.

@Entity
@Table(name = "users", schema = "public", uniqueConstraints = { @UniqueConstraint(columnNames = "email"), @UniqueConstraint(columnNames = "application_id") })
public class Users implements java.io.Serializable
{
    @Id
    @Column(name = "user_id", unique = true, nullable = false)
    private int userId;

    @Column(name = "email", unique = true, nullable = false, length = 100)
    private String email;

}

Now check the schema structure

CREATE TABLE users
(
  email character varying(100) NOT NULL,
  user_id integer NOT NULL,
  CONSTRAINT users_pk PRIMARY KEY (user_id),
  CONSTRAINT unique_email UNIQUE (email)
)

 CREATE TABLE applicant
(
  application_id character(7) NOT NULL,
  email character varying(100) NOT NULL,
  CONSTRAINT users_email_fk FOREIGN KEY (email)
  REFERENCES users (email) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT unique_applicant_email UNIQUE (email)
 )

[Edited] I am using join column on email of user table which has Unique constraint but not it's not PK(PK is user_id)
PS:I can't change db schema structure, I don't have authority

You need to add the referencedColumnName attribute to the @ManyToOne association:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "email", referencedColumnName = "email", unique = true, nullable = false)
private Users usersByEmail;

You defined joinColumn as email, the EMAIL column from your applicant table will be used when creating the joins (which is varchar holding email values). But the joins are done by @ID annotated field, which for user is userId and it is numerical, so you get the exception.

Join is always by @ID, field, the joincolumn tells only where it should be stored in the join or local table.

you need to either define a primary key in the database (instead of multiple unique fields) or use reveng.xml to tell hibernate tools what is the true primary key.

You need something like:

<table name="applicant"> 
 <primary-key>
  <key-column name="application_id"/>
 </primary-key>
</table>

you can see details in the docs at: http://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM