Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Annotation, how to use @Joincolumn refer to a foreign key?
    text
    copied!<p>I use Hibernate 3.6.8. Here is my tables:</p> <pre><code>CREATE TABLE UTILIZATION ( ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY ... FK_WORKCATEGORY SMALLINT, CONSTRAINT PK_UTILIZATION PRIMARY KEY ( ID)); CREATE TABLE DB2ADMIN.WORKCATEGORY( ID SMALLINT NOT NULL, DESCRIPTION VARCHAR(50), CONSTRAINT PK_WORKCATEGORY PRIMARY KEY(ID)); ALTER TABLE UTILIZATION ADD FOREIGN KEY (FK_WORKCATEGORY) REFERENCES WORKCATEGORY(ID); </code></pre> <p>my Pojos:</p> <pre><code>@Entity @Proxy(proxyClass=IWorkCategory.class) @Table(name="WORKCATEGORY") public class WorkCategory extends BoBase implements Serializable, IWorkCategory{ @Id private Integer Id; private String description; @Override public Serializable getId() { return Id; } @Override public void setId(Serializable id) { Id = (Integer)id; } @Override public String getDescription() { return description; } @Override public void setDescription(String description) { this.description = description; } } </code></pre> <hr> <pre><code>@Entity @Proxy(proxyClass=IUtilization.class) @Table(name="UTILIZATION") public class Utilization extends BoBase implements Serializable, IUtilization{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long Id; private WorkCategory workCategory; @ManyToOne(fetch=FetchType.LAZY, targetEntity=WorkCategory.class) @JoinColumn(name="FK_WORKCATEGORY", updatable=false, insertable=false) public WorkCategory getWorkCategory() { return workCategory; } public void setWorkCategory(WorkCategory workCategory) { this.workCategory = workCategory; } } </code></pre> <p>As you can see the table Utilization refer to table <code>WorkCategory</code> by foreign key <code>FK_WORKCATEGORY</code>, but in <code>Utilization</code> pojo, i declare this property with the name <code>workCategory</code>. When I run unit test for <code>Utilization bo = UtilizationDAO.get(new Long(123))</code>, the SQL is generated with the column UTILIZATIO0_.<strong>WORKCATEGORY</strong>, instead of <code>FK_WORKCATEGORY</code>. I have put the <strong>@JoinColumn(name="FK_WORKCATEGORY"</strong>, in the <code>Utilization</code> Pojo, but it doesn't help. Please help me to fix this error. Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload