First attempt gives:
ERROR: duplicate key value violates unique constraint "category_pkey" Detail: Key (id)=(1) already exists.
Hibernate save succeeded only from second attempt.
Latest Hibernate and Spring.
What is wrong?
How fix this?
Main code:
try {
target.save(secondCategory)
} catch (Exception e1) {
try {
target.save(secondCategory)
} catch (Exception e) {
}
System.out.print("------------")//It is being printed during each run well.
}
Category
@Entity
@Table(indexes = {
@Index(name = "nameIndex", columnList = "name")})
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Category implements NamedModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotNull
@Column(length = 256)
@Size(min = 1, max = 256)
private String name;
@Column(length = 512)
@Size(min = 5, max = 512)
private String image;
@JoinColumn(name = "parentId")
@OneToOne(fetch = FetchType.EAGER)
@JsonInclude(JsonInclude.Include.NON_NULL)
private Category parent;
@JsonIgnore
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@OneToMany(cascade = {CascadeType.REMOVE}, orphanRemoval = true, fetch = FetchType.LAZY, mappedBy = "parent")
private List<Category> children;//...
}
Second category
{"id":2,"name":"NAME","image":"IMAGE","parent":{"id":1,"name":"NAME","image":"IMAGE"}}]
CategoryDAO
public class CategoryDAO extends HibernateDaoSupport {
@Transactional
public Serializable save(Entity entity) {
return getHibernateTemplate().save(entity);
}//...
}
Aucun commentaire:
Enregistrer un commentaire