我创建了两个表,即abc和def,我已经使用注释生成了pojo类,但是当我尝试执行项目时,我正在获得mapping异常。任何人都可以告诉我如何为串行nuber作为主键和外键(oneToMany)的表编写pojo。
CREATE TABLE abc
(
abc_id serial NOT NULL,
abc_name character varying(30),
CONSTRAINT abc_pkey PRIMARY KEY (abc_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE abc
OWNER TO postgres;
CREATE TABLE def
(
def_id serial NOT NULL,
abc_id bigint NOT NULL,
CONSTRAINT def_pkey PRIMARY KEY (def_id),
CONSTRAINT def_fkey FOREIGN KEY (abc_id)
REFERENCES abc (abc_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE def
OWNER TO postgres;
和班级,Abc
@Entity
@Table(name = "abc")
public class Abc implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "def_id")
private BigInteger def_id;
@Column(name = "abc_name")
public String abc_name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "abc_id")
List<Def> list;
}
和Def
@Entity
@Table(name = "def")
public class Def implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "def_id")
private BigInteger def_id;
@Column(name = "abc_id")
public BigInteger abc_id;
}
答案 0 :(得分:0)
你做错了。请检查此example
您在 DEF 类中使用注释 @JoinColumn
(....)
@JoinColumn("abc_id")
public BigInteger abc_id_test;
在 ABC 上你应该使用 mappedBy 属性
@OneToMany(mappedBy="abc_id_test")
private List<Def> list;
您可以在我收录的wiki文章中找到更多信息。尝试重写此映射:)