Java 如何使用jsf管理表单上的多对多关系

Java 如何使用jsf管理表单上的多对多关系,java,hibernate,jsf-2,Java,Hibernate,Jsf 2,所以我有这些表格:ocurrencia和persona,它们有多对多的关系。这张表叫做identification。我正在使用hibernate 这是ocurrencia的代码 @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinTable(name = "identificacion", joinColumns = { @JoinColumn(name = "id_ocurrencia",

所以我有这些表格:ocurrencia和persona,它们有多对多的关系。这张表叫做identification。我正在使用hibernate

这是ocurrencia的代码

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "identificacion", joinColumns = { 
        @JoinColumn(name = "id_ocurrencia", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "id_persona", 
                nullable = false, updatable = false) })
private List<Persona> personas
我能想到的唯一解决方案是手动创建一个用于识别的模型类,并与ocurrencia和persona建立多对一关系。我想我必须添加一个主键。但这似乎是一个变通解决方案,如果我们最终这样做,为什么我们会有多对多的选择呢

编辑 我刚找到这个

我没有关于识别的额外专栏,这是解决这个问题的唯一方法吗? 谢谢

更新

我将mappedby=“persona”更改为mappedby=“personas”,现在我发现了这个错误

23:27:20,334 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) ec.edu.puce.biologia.excepciones.EntidadNoGrabadaException: Error al grabar: ec.edu.puce.biologia.model.Ocurrencia@22231361 persistence error org.hibernate.PersistentObjectException: detached entity passed to persist: ec.edu.puce.biologia.model.Persona
23:27:20,335 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at ec.edu.puce.biologia.dao.impl.GenericDao.crear(GenericDao.java:68)
23:27:20,335 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
23:2

发生此错误的原因是您正在持久化一个附加到已存在对象的新对象。
在将
personas
添加到列表之前,在
ocurrence
对象中调用
crear
方法

多亏了fmodos,我才得以修复

我添加了一个角色的tmpList,并将列表设置为null,然后再次将列表添加到ocurrencia,并在创建后更新(实现)我的列表

List<Persona> tmpIdentificadores = new ArrayList<Persona>();
        if(ocurrencia.getPersonas()!=null){
            tmpIdentificadores = ocurrencia.getPersonas();
            ocurrencia.setPersonas(null);
            try {
                ocurrenciaDao.crear(ocurrencia);
            } catch (EntidadNoGrabadaException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ocurrencia.setPersonas(tmpIdentificadores);
            actualizar(ocurrencia);
        }else{
            try {
                ocurrenciaDao.crear(ocurrencia);
            } catch (EntidadNoGrabadaException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
List tmpIdentificadores=new ArrayList();
if(ocurrencia.getPersonas()!=null){
tmpIdentificadores=ocurrencia.getPersonas();
人形眼(空);
试一试{
眼疾;
}捕获(Entidandograbadae例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
眼轮虫;
实施者(眼科);
}否则{
试一试{
眼疾;
}捕获(Entidandograbadae例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

您的字段名是
personas
,但在
mappedBy
中,您使用
persona
,尝试将其更改为
personas
,并让我知道它是否有效。这对我现在发现crear函数中的错误有一点帮助如果您有多对多关系,您应该能够规范化更多,并创建一个1对多关系。@Blaine,基本上是这样的@JuanDiego当您将一个新类附加到一个已经存在的类时,会发生此错误,请尝试使用save方法。。。或者先保持当前状态,然后添加角色并合并它。
23:27:20,334 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) ec.edu.puce.biologia.excepciones.EntidadNoGrabadaException: Error al grabar: ec.edu.puce.biologia.model.Ocurrencia@22231361 persistence error org.hibernate.PersistentObjectException: detached entity passed to persist: ec.edu.puce.biologia.model.Persona
23:27:20,335 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at ec.edu.puce.biologia.dao.impl.GenericDao.crear(GenericDao.java:68)
23:27:20,335 ERROR [stderr] (http-localhost-127.0.0.1-8080-6)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
23:2
List<Persona> tmpIdentificadores = new ArrayList<Persona>();
        if(ocurrencia.getPersonas()!=null){
            tmpIdentificadores = ocurrencia.getPersonas();
            ocurrencia.setPersonas(null);
            try {
                ocurrenciaDao.crear(ocurrencia);
            } catch (EntidadNoGrabadaException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ocurrencia.setPersonas(tmpIdentificadores);
            actualizar(ocurrencia);
        }else{
            try {
                ocurrenciaDao.crear(ocurrencia);
            } catch (EntidadNoGrabadaException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }