当我尝试在数据库中保存下面的实体时,在使用spring和hibernate的应用程序中,以它呈现的方式,我得到错误object references an unsaved transient instance - save the transient instance before flushing
;但是,如果我将值cascade = CascadeType.ALL
添加到注释@OneToOne(或添加注释@Cascade),对于每个属性我都不填充它在数据库中插入一个空记录。
@Entity
@Table(name="cliente")
public class Cliente {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="usuario")
@Order(value=1)
private Usuario usuario;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="pessoa_fisica")
@Order(value=2)
private PessoaFisica pessoaFisica;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="pessoa_juridica")
@Order(value=3)
private PessoaJuridica pessoaJuridica;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="endereco_entrega")
@Order(value=4)
private Endereco endereco_entrega;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="endereco_cobranca")
@Order(value=5)
private Endereco endereco_cobranca;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="pedidos_do_cliente", joinColumns={@JoinColumn(name="fk_cliente")}, inverseJoinColumns={@JoinColumn(name="fk_pedido")})
@Order(value=6)
private List<Pedido> pedido;
}
任何人都可以告诉我如何解决这个问题?
ps:在处理此过程的相关方法下面:
控制器
@RequestMapping(value="cadastra", method=RequestMethod.POST)
@ResponseBody
public String cadastra(@ModelAttribute("object") E object, BindingResult result, @RequestParam(value="file", required=false) MultipartFile file, @RequestParam(value="icone", required=false) MultipartFile icone) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
serv.cadastra(object);
serv.upload_picture(object, file, "picture");
serv.upload_picture(object, icone, "icone");
return "";
}
服务
@Transactional
public void cadastra(E e) {
dao.persist(e);
}
dao class
@Transactional
public void persist(E transientInstance) {
sessionFactory.getCurrentSession().persist(transientInstance);
}
更新
CREATE TABLE cliente
(
id serial NOT NULL,
endereco_cobranca integer,
endereco_entrega integer,
pessoa_fisica integer,
pessoa_juridica integer,
usuario integer,
CONSTRAINT cliente_pkey PRIMARY KEY (id),
CONSTRAINT fk_4taj5h8hxci6slrw0n3d336i7 FOREIGN KEY (endereco_entrega)
REFERENCES endereco (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_7a5l0l5enj00apelvmcdatkmm FOREIGN KEY (pessoa_juridica)
REFERENCES pessoa_juridica (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_iiibo76b56caciax4yl0jo0m6 FOREIGN KEY (pessoa_fisica)
REFERENCES pessoa_fisica (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_lwavy2v0wb0vmxisg7hbe3mbu FOREIGN KEY (usuario)
REFERENCES usuario (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_r2pyppeltv7xoe5quenf2l1gd FOREIGN KEY (endereco_cobranca)
REFERENCES endereco (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE cliente
OWNER TO klebermo;
答案 0 :(得分:0)
在视图层中,您是否为这些链接列创建新对象?或者在为表单提供服务的控制器中,您是否在模型中添加“新”对象?看起来好像你已经为这些一对一列创建了空的java对象,这就是为什么你不能保存父节点而不保存子节点的原因以及为什么使用cascade选项它会创建空记录。