问题
Trying to implement the Bill of Material concept using JPA entity:-
IDE: Eclipse Helios;
Jars: eclipselink2.4.0 , javax.persistence
Entity is as follows:
@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;
}
Output:
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
)
Error: The table is getting created , but the column parent_config_id is missing and its relation to config_id is also missing.
回答1:
You are using @PrimaryKeyJoinColumn(name="config_id") which indicates that the primary key is also a foreign key to the referenced Configuration parent - so it is its own parent. You want to use @JoinColumn to define the foreign key, or leave it blank to have it use the default.
@ManyToOne
@JoinColumn(name="parent_config_id", referencedColumnName="config_id")
private Configuration parent;
来源:https://stackoverflow.com/questions/17923021/jpa-entity-giving-error-on-implementing-bill-of-material-concept