2015-07-10 44 views
0

我有一個創建一個JPA實體與現有的JPA實體分配給@ManyToOneJPA休眠節能多對一字段爲空

當我堅持它,它節省了實體,但外鍵是NULL一個servlet方法。爲什麼?

這裏是我的實體:

@Entity 
public class SimpleEntity implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -5930519292861829894L; 

    @Id @GeneratedValue 
    Long id; 
    String name; 

    @ManyToOne() 
    @JoinColumn(name="simple_entity_group_id", insertable=false, updatable=false, nullable=true) 
    SimpleEntityGroup group; 

    /** 
    * 
    */ 
    public SimpleEntity() { 
    } 
    /** 
    * @return the id 
    */ 
    public Long getId() { 
     return this.id; 
    } 
    /** 
    * @param id the id to set 
    */ 
    public void setId(Long id) { 
     this.id = id; 
    } 
    /** 
    * @return the name 
    */ 
    public String getName() { 
     return this.name; 
    } 
    /** 
    * @param name the name to set 
    */ 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 
    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     return "SimpleEntity [id=" + this.id + ", name=" + this.name + ", group=" + this.getGroup() + "]"; 
    } 
    /** 
    * @return the group 
    */ 
    public SimpleEntityGroup getGroup() { 
     return this.group; 
    } 
    /** 
    * @param group the group to set 
    */ 
    public void setGroup(SimpleEntityGroup group) { 
     this.group = group; 
    } 

} 

@Entity 
public class SimpleEntityGroup implements Serializable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = -1680386377742600266L; 

    @Id @GeneratedValue 
    Long id; 

    String name; 

    @OneToMany(mappedBy="group") 
    java.util.List<SimpleEntity> simpleEntities; 

    /** 
    * 
    */ 
    public SimpleEntityGroup() { 
     simpleEntities = new ArrayList<SimpleEntity>(); 
    } 
    /** 
    * @return the id 
    */ 
    public Long getId() { 
     return this.id; 
    } 
    /** 
    * @param id the id to set 
    */ 
    public void setId(Long id) { 
     this.id = id; 
    } 
    /** 
    * @return the name 
    */ 
    public String getName() { 
     return this.name; 
    } 
    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the simpleEntities 
    */ 
    public java.util.List<SimpleEntity> getSimpleEntities() { 
     return this.simpleEntities; 
    } 
    /** 
    * @param simpleEntities the simpleEntities to set 
    */ 
    public void setSimpleEntities(java.util.List<SimpleEntity> simpleEntities) { 
     this.simpleEntities = simpleEntities; 
    } 

    public void addSimpleEntity(SimpleEntity e) { 
     if(this.getSimpleEntities() != null) { 
      this.getSimpleEntities().add(e); 
      return; 
     } 
     throw new RuntimeException("Entity list is null!!!"); 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     return "SimpleEntityGroup [id=" + this.id + ", name=" + this.name + "]"; 
    } 
    /* (non-Javadoc) 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((this.id == null) ? 0 : this.id.hashCode()); 
     return result; 
    } 
    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     SimpleEntityGroup other = (SimpleEntityGroup) obj; 
     if (this.id == null) { 
      if (other.id != null) { 
       return false; 
      } 
     } else if (!this.id.equals(other.id)) { 
      return false; 
     } 
     return true; 
    } 


} 

這是我如何堅持它:

 SimpleEntity e = new SimpleEntity(); 
    e.setName("Mike"); 
    SimpleEntityGroup g = dao.getGroupById(1l); 
    e.setGroup(g); 
    dao.persist(e); 

    System.out.println(e); 
    System.out.println(dao.findAll()); 

這裏是從Java代碼的輸出,該組設置的條目,但它不是保存。爲什麼?!?!

SimpleEntity [ID = 4,名稱=麥克,基團= SimpleEntityGroup [ID = 1, 名=組1]]

[SimpleEntity [ID = 4,名稱=麥克,基團=空] ]

+0

? – Sekkuar

回答

1

當然我只是想通了,需要做的:

@ManyToOne() 
@JoinColumn(name="simple_entity_group_id") 
SimpleEntityGroup group; 

- 擺脫了插入= false時,更新=假

0

你只發布你的子類,但我認爲如果你還包括父類代碼會更好。當我嘗試使用自動生成的ID進行級聯保存時,我遇到了同樣的問題。我可以使用下一個註釋來解決它。

在我父類我有

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name="IDCOBPRES", unique=true, nullable=false) 
public Long getIdcobpres() { 
    return this.idcobpres; 
} 
//.... 
@OneToMany(fetch=FetchType.LAZY, mappedBy="cobpresGestion") 
@Cascade({CascadeType.ALL}) 
public Set<CobpresOptionDet> getCobpresOptionDets() { 
    return this.cobpresOptionDets; 
} 

在我的孩子I類爲什麼你使用`插入=假,可更新= FALSE`有

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name="IDOPTIONDET", unique=true, nullable=false) 
public Long getIdoptiondet() { 
    return this.idoptiondet; 
} 
//... 
@ManyToOne(fetch=FetchType.LAZY, optional=false) 
@JoinColumn(name="IDCOBPRES", nullable=false, insertable=true, updatable=true) 
public CobpresGestion getCobpresGestion() { 
    return this.cobpresGestion; 
} 
+0

我修好了,看我的帖子。 '擺脫了insert = false,update = false' – mikeb