hibernate4:外键引用的列数错误。应该是2

时间:2014-10-09 12:58:29

标签: java hibernate hibernate-mapping hibernate-annotations

我正在尝试使用hibernate注释为我的数据库表编写模型类。

我有两个表,每个表都有一个主键User和ChartDetails。

enter code here
package com.winnow.springservice.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


//User Entity class mapped with hibernate
@Entity
@Table(name="User")
@SuppressWarnings(value = { "all" })
public class User implements Serializable
{

@Id
@Column(name="user_id")
public String user_Id;

@Column(name="user_name")
public String userName;
public String password;

@Column(name="last_login")
public String last_Login;

@Column(name="role_id")
public int role_Id;
public int status;



/**
 * @return the user_Id
 */
public String getUser_Id() {
    return user_Id;
}
/**
 * @param user_Id the user_Id to set
 */
public void setUser_Id(String user_Id) {
    this.user_Id = user_Id;
}
/**
 * @return the last_Login
 */
public String getLast_Login() {
    return last_Login;
}
/**
 * @param last_Login the last_Login to set
 */
public void setLast_Login(String last_Login) {
    this.last_Login = last_Login;
}
/**
 * @return the role_Id
 */
public int getRole_Id() {
    return role_Id;
}
/**
 * @param role_Id the role_Id to set
 */
public void setRole_Id(int role_Id) {
    this.role_Id = role_Id;
}
/**
 * @return the status
 */
public int getStatus() {
    return status;
}
/**
 * @param status the status to set
 */
public void setStatus(int status) {
    this.status = status;
}
/**
 * @return the userName
 */
public String getUserName() {
    return userName;
}
/**
 * @param userName the userName to set
 */
public void setUserName(String userName) {

    System.out.println("username"+userName);
    this.userName = userName;
}
/**
 * @return the password
 */
public String getPassword() {
    return password;
}
/**
 * @param password the password to set
 */
public void setPassword(String password)
{


    System.out.println("password "+password);
    this.password = password;
}


}

图表详情

enter code here
package com.winnow.springservice.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Chart_Details")
@SuppressWarnings(value = { "all" })
public class ChartDetails implements Serializable
{

@Id
@Column(name="chart_id")
public int chartId;

@Id
@Column(name="chart_type_id")
public int chartTypeId;

@Column(name="chart_name")
public String chartName;

@Column(name="x_axis")
public String x_axis;

@Column(name="y_axis")
public String y_axis;

@Column(name="z_axis")
public int z_axis;

@Column(name="chart_filter_id")
public int chartFilterId;

@Column(name="is_data_table")
public boolean isDataTable;

@Column(name="dataset_id")
public int datasetId;

@Column(name="user_id")
public int userId;


@Column(name="project_id")
public int projectId;

}

我还有一个桌子 - ChartsStarredBy,它有userId和chart_id作为上述两个表中的外键。

但是我无法找到如何在ChartsStarredBy表中引用这些约束。

enter code here
package com.winnow.springservice.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


@Entity
@Table(name="Chart_Starred_By")
@SuppressWarnings(value = { "all" })
public class ChartsStarredBy implements Serializable
{

@Id
public int id;

@Temporal(TemporalType.DATE)
public Date starred_date;


@ManyToOne
@JoinColumn(name = "FK_chart_id4")
private  ChartDetails chart_details;

@ManyToOne
@JoinColumn(name = "FK_user_id4")
private User user;

 public Date getStarred_date() 
{
    return starred_date;
}

public void setStarred_date(Date starred_date) 
{
    this.starred_date = starred_date;
}



    public User getUser() 
    {
    return user;
    }

public void setUser(User user) 
{
    this.user = user;
}

public ChartDetails getChart_details() {
    return chart_details;
}

public void setChart_details(ChartDetails chart_details) {
    this.chart_details = chart_details;
}

}

请帮我实现这个目标?提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您应该为User表创建一个整数id,它是主键。在您的代码中,您将其创建为字符串类型。

你这里做错了

@ManyToOne
@JoinColumn(name = "FK_chart_id4")
private  ChartDetails chart_details;

@ManyToOne
@JoinColumn(name = "FK_user_id4")
private User user;

您在“用户”表格中有ID& ChartDetails表格为

@Id
@Column(name="user_id")
public String user_Id;

AND

@Id
@Column(name="chart_id")
public int chartId;

因此,当您引用另一个类的ID时,您应该在 @JoinColumn 中提供该ID的名称。就像这样

@ManyToOne
@JoinColumn(name = "chartId")
private  ChartDetails chart_details;

@ManyToOne
@JoinColumn(name = "user_Id")
private User user;

你也可以像这样使用Hibernate中的 @ForeignKey(name =" user_Id")注释

@ManyToOne
@ForeignKey(name = "user_Id")
private User    user;

示例教程: - Hibernate One To Many Annotation Tutorial