我正在用JPA做数据库。我定义了类,现在我正在插入数据。这是我的表:插入JPA a集合
public class Cliente {
@Id
@Column(name = "DNI", nullable = false, length = 10)
private String DNI;
@Column(name= "Nombre", nullable = false, length= 20)
private String nombre;
@Column(name= "Apellidos", nullable = false,length= 50)
private String apellidos;
@Column(name= "FechaNac" , nullable = false)
private Date FechaNac; //uso Date de SQL ya que es para una base de datos.
@Column(name= "Direccion" , nullable = false, length = 40)
private String direccion;
@Column(name = "email" , nullable= true, length=50)
private String email;
@Column(name = "Telefono" , nullable=false,length=15)
private String telefono;
@ManyToMany(cascade ={CascadeType.ALL})
@JoinTable(name="es_titular",[email protected](name="DNI"),[email protected](name="numerocuenta"))
private Set<Cuenta> cuentas;
public Cliente(){}
public Set<Cuenta> getCuentas(){
return cuentas;
}
public void setCuentas(Set<Cuenta> a){
cuentas=a;
}
}
和其他表:
package Entidades;
import java.sql.Date;
import java.util.Set;
import javax.persistence.*;
@Entity(name="Cuenta")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(
name="discriminator",
discriminatorType=DiscriminatorType.STRING)
//Si es especializacion total, no se pone.
//@DiscriminatorValue("D")
public class Cuenta {
@Id
@Column(name="numerocuenta", nullable=false, length=15)
private String numerocuenta;
@Column(name="FechaCreacion", nullable=false)
private Date FechaCreacion;
@Column(name="Saldo", nullable=false, precision=2)
private float Saldo;
@ManyToMany(mappedBy ="cuentas")
private Set<Cliente> clientes;
@OneToMany(cascade=CascadeType.REMOVE, mappedBy="cuenta")
private Set<Operacion> operaciones;
@OneToMany(cascade=CascadeType.REMOVE, mappedBy="cuentaObjetivo")
private Set<Transferencia> transferencias;
public Set<Cliente> getClientes(){
return clientes;
}
public void setClientes(Set<Cliente> s){
clientes=s;
}
public Set<Operacion> getOperaciones(){
return operaciones;
}
public void setOperaciones(Set<Operacion> s){
operaciones=s;
}
public Set<Transferencia> getTransferencias(){
return transferencias;
}
public void setTransfrencias(Set<Transferencia> s){
transferencias=s;
}
}
我消除了一些属性,以使德代码更干净。
然后,当我插入Cuentas,我是这样做:
Set<Cliente> clientes=new HashSet<Cliente>();
clientes.add(cliente1);
clientes.add(cliente2);
trans.begin();
Cuenta cuenta = new Cuenta();
cuenta.setNumerocuenta("343434");
cuenta.setFechaCreacion(new Date(2013,05,05));
cuenta.setSaldo(0);
cuenta.setClientes(clientes);
em.persist(cuenta);
trans.commit();
随着Cliente和CUENTA创建。但是当我查看数据库时,我没有看到任何表中的Set Clientes。
你知道我在做什么错吗?
编辑:我的目标是当我在连接表es_titular中的任何表(Cliente或Cuenta)中插入Set时出现。 es_titular的结构是:DNI varchar2(10个字符),numerocuenta varchar2(15个字符)
它应该在您的数据库上有一个Cliente表。请检查。 – fmodos 2013-05-07 20:01:43
在我的数据库中,我有一个Cliente表和一个Cuenta表。 – Elseine 2013-05-07 20:07:06
所以套件中的Clientes应该插入该表中。 – fmodos 2013-05-07 20:12:45