Hi,
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.