2013-05-07 57 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