gpt4 book ai didi

java - hibernate : One Entitiy class for multiple purpose?

转载 作者:行者123 更新时间:2023-11-29 07:33:46 25 4
gpt4 key购买 nike

我有一个名为 Category 的实体类。我之前用它来连接另一个实体。现在我必须使用相同的 Category 实体将数据插入 类别表。

Category.java

@Entity
@Table(name = "category")
public class Category implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private Integer categoryId;

@Column(name = "category_name")
private String categoryName;

@NotFound(action=NotFoundAction.IGNORE)
@JsonIgnore
@ManyToOne
@JoinColumn(name = "parent_category_id")
private Category parentCategory;

@Column(name = "created_date")
@Temporal(javax.persistence.TemporalType.DATE)
private Date createdDate;

@Column(name = "last_updated_date")
@Temporal(javax.persistence.TemporalType.DATE)
private Date lastUpdatedDate;

@OneToMany(mappedBy = "category")
private List<Events> events;

//Getters and Setters
}

Method for insert category

 @Transactional
public UserResponse createCategory(SubmitReviewRequest createCategoryRequest) throws SQLException, ClassNotFoundException, IOException {
Category category = new Category();
UserResponse userResponse = new UserResponse();
if (createCategoryRequest != null) {
category.setCategoryName(createCategoryRequest.getSubCategory());
// category.setParenCategoryId(Integer.parseInt(createCategoryRequest.getMainCategory()));
int id = adminServiceDao.saveCategory(category);
userResponse.setCode(WeekenterConstants.SUCCESS_CODE);
userResponse.setMessage("Success");
userResponse.setId(id);
}

return userResponse;
}

问题是我必须在 Category 实体中设置 parent_category_id 才能将数据保存到 Category 表中,但我使用哪一列来加入表之前。

Table

enter image description here

我可以在 Category 实体中设置除 parent_category_id 之外的所有值,即 categoryId,categoryName,createdDate,lastUpdatedDate t 创建 getters 和 setters。当我尝试创建时,它显示错误 parent_category_id 列名称已使用。此错误的原因是我已使用它来加入。

Help me for use the same entity for insertion also or tell me will i need to create a separate entity for that which am not feel appropriate ?

User request sample :

{
"categoryName":"cricket",
"parentCategoryId":15
}

最佳答案

如果我理解正确的话,那么你的做法是错误的。在 hibernate 中,正确的方法是读取父类别并使用其ID(在您的情况下为1)拥有一个持久的父类别对象,然后将您尝试保留的该类别设置为parentCategory.setParentCategory(category).

@Transactional
public UserResponse createCategory(SubmitReviewRequest createCategoryRequest) throws SQLException, ClassNotFoundException, IOException {
Category category = new Category();
// read parent
Category parentCategory = adminServiceDao.find(Integer.parseInt(createCategoryRequest.getMainCategory()));
UserResponse userResponse = new UserResponse();
if (createCategoryRequest != null) {
category.setCategoryName(createCategoryRequest.getSubCategory());
// category.setParenCategoryId(Integer.parseInt(createCategoryRequest.getMainCategory()));
//set the child relationship.
category.setParentCategory(parentCategory);
int id = adminServiceDao.saveCategory(parentCategory);
userResponse.setCode(WeekenterConstants.SUCCESS_CODE);
userResponse.setMessage("Success");
userResponse.setId(id);
}

return userResponse;
}

关于java - hibernate : One Entitiy class for multiple purpose?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com