last day (14 days later) » 

9:47 AM
0
A: Hibernate : One Entitiy class for multiple purpose?

RohitIf 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.setParentCat...

 
See my user request in question with parentCategoryId how can i insert that parentCategoryId which is not auto generated ?
Oki your trying to say i sholud find the parentCategoryId rather than trying to insert the value fetching from user request ?
 
You are mixing JDBC based aproach and ORM based approach. In ORM you think about relationship.
Yes fetch the parent and then persist the child in relation parent.
 
it might be the standard way..but , I have all the info which i need to insert ..then why i have to fetch it again ? will i can set directly and insert without find parentCatogry ?
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.weekenter.www.entity.Category Am getting the error when trying this
 
This can be solved by either persisting the category before without setting parent reference or defining a cascade attribute like @ManyToOne(cascade = CascadeType.ALL)
 
Hii
 
9:52 AM
Hi
 
now am getting the error parent_category_id canot be null
becoz i dont have a property in category like parent_category_id
how can i solve this issue please help me out
??
r u dere?
 
10:12 AM
Hlw
 
10:24 AM
Sorry, had to go away.
Did you check the DB is the parent_category_id column is nullable?
 
yup the column can't be null
 
Try @ manytoOne cascade attribute
 
it' s manytoone only
@NotFound(action = NotFoundAction.IGNORE)
@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_category_id")
private Category parentCategory;
can i ask you one doubt ?
we aren't defined any property for parent_category_id then how it can be insert after fetches from the request / the way u said from db ... ?
it would be null in that case right?
 
ther was a typo in the code. Its should be other way around category.setParentCategory(parentCategory);
I have question regarding why parent_category_id column can't be null? Does the topmost category have a parent?
 
10:40 AM
in that table every record should maintain a parent_category_id
it can't be null
in that table first 5 record is the main category
 
and what is there parent_id?
 
it will keep as 0
other record is the sub category
 
Ok. So did you try with cascade?
 
and each the sub category have parent_category_id as 1-5
check this link for my full table view
yup i tried with cascade am getting the error parent_category_id canot be null
 
ther was a typo in the code that i suggested. It should be other way around category.setParentCategory(parentCategory);
Did you try this?
 
10:47 AM
ohhh wait a moment
:)
now no error but the records hasn't inserted
 
can you chare the createcategory method?
 
@Transactional
public UserResponse createCategory(SubmitReviewRequest createCategoryRequest) throws SQLException, ClassNotFoundException, IOException {

Category category = new Category();
// read parent
Category parentCategory = adminServiceDao.findCategory(createCategoryRequest.getMainCategory());
UserResponse userResponse = new UserResponse();
if (createCategoryRequest != null) {
category.setCategoryName(createCategoryRequest.getSubCategory());
System.out.println("Sub = " + createCategoryRequest.getSubCategory());
 
you are still persisting the parent and not the child
adminServiceDao.saveCategory(category);
use the above code
 
check now it' s oki??
@Transactional
public UserResponse createCategory(SubmitReviewRequest createCategoryRequest) throws SQLException, ClassNotFoundException, IOException {

Category category = new Category();
// read parent
Category parentCategory = adminServiceDao.findCategory(createCategoryRequest.getMainCategory());
UserResponse userResponse = new UserResponse();
if (createCategoryRequest != null) {
category.setCategoryName(createCategoryRequest.getSubCategory());

//set the child relationship.
category.setParentCategory(parentCategory);
 
seems to be.
 
10:59 AM
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'created_date' cannot be null
i sholud set the created date /
??
 
the exception does seem to suggest that the column is required.
 
yup i got
what will do ?
i sholud set ??
 
yes shoudl
 
oki i will try
done it...but plz tell me where from we get paret_cat_id ?
may the cascade will effect my result fetch ??
i mean previous operation ?
can u please explain what we did finally
??
as finally
 
:)
I would suggest you to read up on ORM. You were trying to persist like you would do in JDBC.
 
11:14 AM
i read already .can u tell where from we got parent_id in fact there dont have a property like parent_id
we used that one for join only
??
 
The part we chaned was to persist category in relation to parent category.
hibernate takes care of it.
 
can u give me a good link to learn all these thing perfectly
please update the answer i will accept it
 
already did
 
oki if u dont mind please get me some links for learn it perfctly
 
 
1 hour later…
12:52 PM
May be this helps you
 

last day (14 days later) »