2013-05-07 104 views
0

我正在用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个字符)

+0

它应该在您的数据库上有一个Cliente表。请检查。 – fmodos 2013-05-07 20:01:43

+0

在我的数据库中,我有一个Cliente表和一个Cuenta表。 – Elseine 2013-05-07 20:07:06

+0

所以套件中的Clientes应该插入该表中。 – fmodos 2013-05-07 20:12:45

回答

1

您有一个双向关联。每个双向关联都有一个所有者和一个反面。反面是mappedBy属性的一面。所有者方是另一方。 JPA不关心反面。

因此,如果您想将客户与提示相关联,则必须将提示添加到客户,因为cuente是该关联的所有者方。良好做法,一般,是维持协会的两侧,即使JPA只关心业主方,因为

  • 你一定不会对业主方
  • 忘记它使对象图一致
+0

同时维护,你的意思是在两个表中创建@JoinColumn? – Elseine 2013-05-08 01:07:47

+0

不,我的意思是,如果您将一个客户添加到队列中,您还应该向客户添加队列,以获得一个连贯的对象图。 – 2013-05-08 06:29:29