Reputation:
Recently i am started using hibernate. to insert or update i am using saveOrUpdate()
function.
If i update an entry i works fine. But, with new entry hibernate generate a Query. Buts nothing gets updated in the table.
My scenario is like this,
i am using Many to One & One to Many relationship between two tables[Expense, Category]
For update or insert, i am creating two objects(Expense expense, Category category)
from the
client side. In the server side, i set category
object with expense
object.
public void upDateExpenseTable(Expens expense, Category category) {
expense.setCategory(category);
Session session = Main.getSession();
try{
System.out.println("Inside Update Try Block");
session = Main.getSession();
Transaction tr = session.beginTransaction();
session.saveOrUpdate(expense);
tr.commit();
}
finally{
session.close();
}
}
Object structure is like, catogery.catId, category.catName
&
expense.expnsId, expense.expnsName, expense.amount, expense.status, expense.userName
.
But there is another one column in Expense
Table cat_id
. Its is through mapping annotation. But i dont have any property for that in Expense entity.
When inserting new data, i am not giving any Id.
Any suggestions!!!
public class Expens implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int expnsId;
private int amount;
private String date;
private String status;
private String userName;
@ManyToOne
@JoinColumn(name = "cat_id")
private Category category;
//Setter Getter
}
Category Classs
public class Category{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;
@OneToMany(targetEntity=Expens.class, mappedBy = "category", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Expens> expenses;
}//Setters Getters ommited.
Upvotes: 2
Views: 3978
Reputation: 570595
I'm not sure I understood everything but I can at least tell you that you're not building your bi-directional association correctly, you need to set "both sides of the link" when building your object graph:
...
expense.setCategory(category);
category.getExpenses().add(expense);
...
session.saveOrUpdate(category);
And this is usually done in "link management" methods, like this (in Category
):
@Entity
public class Category {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;
@OneToMany(targetEntity=Expens.class, mappedBy = "category", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Expens> expenses = new ArrayList<Expens>();
public void addToExpenses(Expense expense) {
expenses.add(expense);
expense.setCategory(this);
}
protected List getExpenses() { // protected to prevent direct access from outside the hierarchy
return expenses;
}
protected void setExpenses(List expenses) { // protected to prevent direct access
this.expenses = expenses;
}
//...
}
And the code becomes:
category.addToExpenses(expense);
session.saveOrUpdate(category);
Funnily enough (or not), I've written about this three times today.
Upvotes: 1