1

尝试使用 JPA 实体实现物料清单概念:-
IDE:Eclipse Helios;
jars: eclipselink2.4.0 , javax.persistence 实体如下:

@Id
@TableGenerator(name = "Config_Key_Incrementor", table = "id_generator", pkColumnName = "gen_name", valueColumnName = "gen_value", pkColumnValue = "conifg_id_gen", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = TABLE, generator = "Config_Key_Incrementor")
@Column(name = "config_id")
private int configId;

@Column(name = "config_name")
private String configName;

//bi-directional many-to-one association to Bill_Of_Material
@ManyToOne
@PrimaryKeyJoinColumn(name="config_id")
private Configuration parent;

//bi-directional many-to-one association to Bill_Of_Material
@OneToMany(mappedBy="parent")
private List<Configuration> children = new ArrayList<Configuration>();

public Configuration getParent() {
    return parent;
}

public void setParent(Configuration parent) {
    this.parent = parent;
}



public List<Configuration> getChildren() {
    return children;
}

public void setChildren(List<Configuration> children) {
    this.children = children;
}

public int getConfigId() {
    return configId;
}

public void setConfigId(int configId) {
    this.configId = configId;
}

public String getConfigName() {
    return configName;
}

public void setConfigName(String configName) {
    this.configName = configName;
}

输出:

  CREATE TABLE configuration    
  (    
  config_id integer NOT NULL,  
  config_name character varying(255),    
  CONSTRAINT configuration_pkey PRIMARY KEY (config_id ),  
  CONSTRAINT fk_configuration_config_id FOREIGN KEY (config_id)  
         REFERENCES configuration (config_id) MATCH SIMPLE  
         ON UPDATE NO ACTION ON DELETE NO ACTION  
  )

错误: 正在创建表,但缺少 parent_config_id 列,并且它与 config_id 的关系也缺失。

4

1 回答 1

0

您正在使用 @PrimaryKeyJoinColumn(name="config_id") ,这表明主键也是引用的 Configuration 父级的外键 - 所以它是它自己的父级。您想使用 @JoinColumn 来定义外键,或者将其留空以使其使用默认值。

@ManyToOne
@JoinColumn(name="parent_config_id", referencedColumnName="config_id")
private Configuration parent;
于 2013-07-29T12:35:00.270 回答