Stella
Stella

Reputation: 1817

Hibernate : One Entitiy class for multiple purpose?

I have an entity class named Category.I used it before for joining with another entity.now i have to use the same Category entity for inserting data into Category table.

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;
    }

The problem is i have to set parent_category_id in Category entity for saving data into Category table but which column i used for joining the table before.

Table

enter image description here

I can set all the values ie , categoryId,categoryName,createdDate,lastUpdatedDate in the Category entity other than parent_category_id which i can't create getters and setters.when i tried to create it's showing the error parent_category_id column name already used.the reason for this error is i have used it for joining.

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
}

Upvotes: 0

Views: 83

Answers (2)

Rohit
Rohit

Reputation: 2152

If I understand you correctly, you are going about it incorrectly. In hibernate the right way to do it would be to read the parent Category and have a persistent parent Category object using its ID(1 in your case) and then set this Category you are trying to persist as 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;
    }

Upvotes: 1

Mare Geldenhuys
Mare Geldenhuys

Reputation: 189

Look up the parent category entity from your dao and then set the parent entity using the setter for setting the parent category instead of trying to set the parent category id. For example

Category parentCategory = adminServiceDao.findCategory(Integer.parseInt(createCategoryRequest.getMainCategory());
category.setParentCategory(parentCategory);

Upvotes: 1

Related Questions