-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: inserting a new row into a list hibernate query
PostPosted: Fri May 07, 2010 4:51 pm 
Newbie

Joined: Fri Oct 30, 2009 4:30 pm
Posts: 16
Hi

I have a object A which has one many relationship with object B. A->Student, B->ConcentrationPlan

now if I add a new concentration to the list.

I do the following in terms of code
selectedStudent.getConcentrationPlan.add(concentrationPlan);
studentDao.flush();
loadConcentrationPlans();

selectedStudent is of type Student.
It will get the list of Concentrations for a Student.
And adds the new one to the list.

After I add the new object to the list, and flush it into DB, I notice hibernate query generated for this statement.

Hibernate generates a insert query as expected.

But what surprises me is that it generates a update query as well.

Please let me know why this is happening.

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "SUBJECT_ID", referencedColumnName = "SUBJECT_ID")
@OrderBy(clause = "course_no")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public List<ConcentrationPlan> getConcentrationPlans() {
return this.concentrationPlans;

public void setConcentrationPlans(List<ConcentrationPlan> concentrationPlans) {
this.concentrationPlans = concentrationPlans;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SUBJECT_ID", referencedColumnName = "SUBJECT_ID", nullable = false, insertable = false, updatable = false)
public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

The One to many from Student to ConcentrationPlan is given above. Also the ManytoOne in ConcentrationPlan is also given.

I will also put in the hibernate query I see in the console.

insert
into
KARYN.CONCENTRATION_PLAN
(CONC_AREA_CODE, COURSE_TITLE, DATE_ENTERED, DATE_UPDATED, ENTERED_BY, TAKEN, UNIT, UPDATED_BY, COURSE_NO, SUBJECT_ID)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

16:48:33,209 INFO [STDOUT] Hibernate:
update
KARYN.CONCENTRATION_PLAN
set
SUBJECT_ID=?
where
COURSE_NO=?
and SUBJECT_ID=?
}


The update query is the one that does not make sense.

One more thing I have to mention.

I do call the method loadConcentrationPlans if you notice above in the code.

public void loadConcentrationPlans() {

concentrationList = selectedStudent.getConcentrationPlans();
/*Set<ConcentrationPlan> concentrationSet = selectedStudent.getConcentrationPlans();
concentrationList = new ArrayList<ConcentrationPlan>(concentrationSet);*/

if (concentrationList.size() == 0) {
mode = "add";
concPlan = new ConcentrationPlan();

}else{
mode = "view";
}

}

This method basically calls the view page to display the list by querying the DB for a particular student.



Please let me know what could be happening.

thanks
Sai


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.