休眠持久化,但不在子级中添加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)并且,持久化来自User的所有对象。

我在这段代码中发现的问题是,hibernate Template.save不返回ID(oracle),因为它返回与hibernate的ID关系。如果我不将调试eclipse中热模式下的返回ID更改为数据库中的真实ID,那么抛出FK异常,为什么关联或用户的ID与数据库中的实际ID无关。

解决方案"休眠不从带有序列和触发器的Oracle返回真实ID"

此处的解决方案

要持久化双向关系,首先要设置关系的两侧,然后保存对象。

最新更新