我试图坚持两个对象产品和关系中的类别。 我可以坚持类别而不是产品: 我有这种例外
During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.backupapi.entity.Categorie[ idCat=1libelle=Gamme Azurdescription= ].
这是类产品
public class Produits implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_Prod")
private Integer idProd;
@Basic(optional = false)
@Column(name = "libelle")
private String libelle;
@Basic(optional = false)
@Column(name = "prixUnitaire")
private float prixUnitaire;
@Basic(optional = false)
@Column(name = "datePeremption")
@Temporal(TemporalType.DATE)
private Date datePeremption;
@JoinColumn(name = "id_Cat", referencedColumnName = "id_Cat")
@ManyToOne(optional = false)
private Categorie idCat;
public String toString() {
return " com.backupapi.entity.Produits[idProd=" + idProd + " libelle="+libelle+" prix Unitaire="+prixUnitaire+" date Peremption="+datePeremption+"id_cat="+idCat+" ]";
}
这是班级类别
public class Categorie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_Cat")
private Integer idCat;
@Basic(optional = false)
@Column(name = "libelle")
private String libelle;
@Basic(optional = false)
@Column(name = "description")
private String description;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idCat")
private List<Produits> produitsList;
@Override
public String toString() {
return "com.backupapi.entity.Categorie[ idCat=" + idCat +"libelle="+libelle+"description="+description+" ]";
}
这是持久化的Jpa方法 Iam坚持列表元素
public void createCategorie(List<Object> objectList) {
//boolean bool=false;
for(Object listElement:objectList){
try {
this.em.persist(listElement);
} catch (Exception e) {
e.printStackTrace();
}
//if (listElement==null){bool= false;}else{bool= true;}
}
是否有主要方法
for (Class entiClass : generic.allClassname()) {
List<Object> Objectlist = new ArrayList<>();
JAXBContext context = JAXBContext.newInstance(entiClass);
Objectlist = entityListfromxml(context, entiClass, "test.xml");
System.out.println(Objectlist.toString());
genericRestore.createCategorie(Objectlist);
// System.out.println(Objectlist.toString());
//System.out.println(test);
}
执行它是向我发送方法tostring
的原因[com.backupapi.entity.Categorie[ idCat=1libelle=Gamme Azurdescription= ], com.backupapi.entity.Categorie[ idCat=2libelle=Gamme Propredescription= ], com.backupapi.entity.Categorie[ idCat=3libelle=Gamme alimentairedescription=null ]]
[ com.backupapi.entity.Produits[idProd=1 libelle=HP-PC prix Unitaire=200000.0 date Peremption=Tue Jul 19 00:00:00 UTC 2016id_cat=com.backupapi.entity.Categorie[ idCat=1libelle=Gamme Azurdescription= ] ], com.backupapi.entity.Produits[idProd=2 libelle=Saucisse prix Unitaire=500.0 date Peremption=Thu Jul 21 00:00:00 UTC 2016id_cat=com.backupapi.entity.Categorie[ idCat=3libelle=Gamme alimentairedescription=null ] ], com.backupapi.entity.Produits[idProd=3 libelle=Gateau prix Unitaire=25.0 date Peremption=Tue Jul 12 00:00:00 UTC 2016id_cat=com.backupapi.entity.Categorie[ idCat=3libelle=Gamme alimentairedescription=null ] ]]
我使用元数据来获得项目中的所有classe。
那么,如何坚持产品