0
我嘗試一個用戶可以成爲幾個關聯的成員。要做到這一點,我創建:休眠持久性,但不添加兒童FK
用戶
@Id
@SequenceGenerator(name="seqUsuario",sequenceName="SQ_USUARIO")
@GeneratedValue(generator="seqUsuario",strategy=GenerationType.SEQUENCE)
private Integer idUsuario;
@Column(name="NOMBRE",nullable=false)
private String nombre;
@Column(name="EMAIL",nullable=false)
private String email;
@Column(name="CLAVE",nullable=false)
private String clave;
@Column(name="APELLIDOS",nullable=false)
private String apellidos;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="IDCALLE")
private Calle calle;
@OneToMany(mappedBy="usuario",cascade=CascadeType.ALL,orphanRemoval=true)
private List<QSM> qsms;
@OneToMany(mappedBy="usuario",cascade=CascadeType.ALL,orphanRemoval=true)
private Set<Miembro> miembros;
協會
@Id
@SequenceGenerator(sequenceName="SQ_ASOCIACION",name="seqAsociacion")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seqAsociacion")
private Integer idAsociacion;
@Column(name="NOMBRE",nullable=false)
private String nombre;
@OneToMany(mappedBy="asociacion",cascade=CascadeType.ALL,orphanRemoval=true)
private Set<Miembro> miembros;
會員
@Id
@SequenceGenerator(sequenceName="SQ_MIEMBRO",name="seqMiembro")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seqMiembro")
private Integer idMiembro;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="IDASOCIACION")
private Asociacion asociacion;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="IDUSUARIO")
private Usuario usuario;
SQL:會員
+----------------+---------+------+
| Field | Type | Null |
+----------------+---------+------+
| IDMIEMBRO | int(11) | NO |
| IDUSUARIO | int(11) | YES |
| IDASOCIACION | int(11) | YES |
+----------------+---------+------+
方法
public void nuevoUsuario(){
//Creamos el miembro para introducirlo en la A.A.V.V
Miembro miembro=new Miembro();
miembro.setActivado(true);
//Obtenemos el resourcebundle para los mensajes predefinidos
ResourceBundle recurso=ResourceBundle.getBundle("org.pfc.mensajes.message");
//Si la asociación vecinal no existe, se crea y el usuario pasa a presidente
Asociacion asociacion=new Asociacion();
if(usuario.getCalle().getBarrio().getAsociacion()==null){
asociacion.setActivado(true);
asociacion.setPrivado(false);
asociacion.setNombre("A.A.V.V." + usuario.getCalle().getBarrio().getNombre());
asociacion.setFecha(Calendar.getInstance());
asociacion.setDescripcion(MessageFormat.format(recurso.getString("asociacion.descripcion"),usuario.getCalle().getBarrio().getNombre()));
TipoAsociacion tipoAsociacion=asociacionBo.getTipoAsociacionByString("VECINAL");
asociacion.setTipoAsociacion(tipoAsociacion);
miembro.setAsociacion(asociacion);
asociacionBo.save(asociacion);
}
else{
//Si la asociación existe se añade al usuario como miembro
usuario.getCalle().getBarrio().getAsociacion().getMiembros().add(miembro);
}
usuarioBo.save(usuario);
miembro.setUsuario(usuario);
miembroBo.save(miembro);
}
這樣,我救三個對象。但是,我的想法是不叫miembroBo.save(miembro)而且,堅持從用戶的所有對象。
我在這段代碼中發現的問題是,HibernateTemplate.save不會返回ID(oracle),因爲它返回與hibernate的ID關係,如果我不更改在調試eclipse中的熱模式返回ID到真實ID數據庫,然後拋出FK異常,爲什麼分離或用戶的Id與數據庫中的真實ID無關。
解「休眠不從Oracle具有序列和觸發器返回真實ID」