Spring MVC с отображением отношений hibernate @onetomany
I have two table one is Credit and second is Debit. I want @OneToMany relationship. in credit table only single row of data and In debit table multiple row of data Credit table: cid openingBalance date debittotal drawertotal debittotalplusdrawertotal todaybusiness all row of only single row data Debit table: did amounnt description amount and description multiple row data I am using Spring mvc with hibernate project structure is just like below controller entity dao daoImpl service serviceImpl How to create model with @OneToMany Relationship and when I save that data then all data will save at time into two table I ave try but only credit table of data is inserted but debit table data is not inserted
Что я уже пробовал:
package com.rojmat.entity; import java.util.Date; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.Type; import org.springframework.core.annotation.Order; import org.springframework.format.annotation.DateTimeFormat; @Entity @Table(name="credit") public class Credit extends BaseEntity{ @Id @Column @GeneratedValue(strategy=GenerationType.AUTO) private long cid; @Column @Order private long openingbalance; @Column @Type(type="date") private Date date; @Column @Order private long debittotal; @Column @Order private long drawertotal; @Column @Order private long debittotalplusdrawertotal; @Column @Order private long todaybusiness; @OneToMany(mappedBy = "credit", cascade = CascadeType.ALL) private List<Debit> debit; public Credit() { } public Credit(long cid, long openingbalance, Date date, long debittotal, long drawertotal, long debittotalplusdrawertotal, long todaybusiness, List<Debit> debit) { super(); this.cid = cid; this.openingbalance = openingbalance; this.date = date; this.debittotal = debittotal; this.drawertotal = drawertotal; this.debittotalplusdrawertotal = debittotalplusdrawertotal; this.todaybusiness = todaybusiness; this.debit = debit; } public long getCid() { return cid; } public void setCid(long cid) { this.cid = cid; } public long getOpeningbalance() { return openingbalance; } public void setOpeningbalance(long openingbalance) { this.openingbalance = openingbalance; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public long getDebittotal() { return debittotal; } public void setDebittotal(long debittotal) { this.debittotal = debittotal; } public long getDrawertotal() { return drawertotal; } public void setDrawertotal(long drawertotal) { this.drawertotal = drawertotal; } public long getDebittotalplusdrawertotal() { return debittotalplusdrawertotal; } public void setDebittotalplusdrawertotal(long debittotalplusdrawertotal) { this.debittotalplusdrawertotal = debittotalplusdrawertotal; } public long getTodaybusiness() { return todaybusiness; } public void setTodaybusiness(long todaybusiness) { this.todaybusiness = todaybusiness; } public List<Debit> getDebit() { return debit; } public void setDebit(List<Debit> debit) { this.debit = debit; } }
package com.rojmat.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.springframework.core.annotation.Order; @Entity @Table(name="debit") public class Debit { @Id @Column @GeneratedValue(strategy=GenerationType.AUTO) private int did; @Column @Order @JoinColumn(name="cid") private int cid; @Column @Order private long amount; @Column @Order private String description; public Debit() { } public Debit(int did, int cid, long amount, String description) { super(); this.did = did; this.cid = cid; this.amount = amount; this.description = description; } public int getDid() { return did; } public void setDid(int did) { this.did = did; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public long getAmount() { return amount; } public void setAmount(long amount) { this.amount = amount; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
package com.rojmat.serviceImpl; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.rojmat.dao.CreditDao; import com.rojmat.entity.Credit; import com.rojmat.entity.User; import com.rojmat.service.CreditService; @Service @Transactional public class CreditServiceImpl implements CreditService { @Autowired private CreditDao creditDao; @Override public void addCreditDebit(Credit credit) { Credit creditdebit = new Credit(); User user = new User(); creditdebit.setDebit(credit.getDebit()); creditdebit.setOpeningbalance(credit.getOpeningbalance()); creditdebit.setDate(credit.getDate()); creditdebit.setDebittotal(credit.getDebittotal()); creditdebit.setDrawertotal(credit.getDrawertotal()); creditdebit.setDebittotalplusdrawertotal(credit.getDebittotalplusdrawertotal()); creditdebit.setTodaybusiness(credit.getTodaybusiness()); creditdebit.setCreatedBy(user.getEmail()); creditdebit.setCreatedDate(new Date()); creditdebit.setUpdatedBy(user.getEmail()); creditdebit.setUpdatedDate(new Date()); creditDao.addCreditDebit(creditdebit); } @Override public void updateCreditDebit(Credit credit) { Credit creditdebit = new Credit(); User user = new User(); creditdebit.setDebit(credit.getDebit()); creditdebit.setOpeningbalance(credit.getOpeningbalance()); creditdebit.setDate(credit.getDate()); creditdebit.setDebittotal(credit.getDebittotal()); creditdebit.setDrawertotal(credit.getDrawertotal()); creditdebit.setDebittotalplusdrawertotal(credit.getDebittotalplusdrawertotal()); creditdebit.setTodaybusiness(credit.getTodaybusiness()); creditdebit.setCreatedBy(user.getEmail()); creditdebit.setCreatedDate(new Date()); creditdebit.setUpdatedBy(user.getEmail()); creditdebit.setUpdatedDate(new Date()); creditDao.updateCreditDebit(creditdebit); } @Override public void deleteCreditDebit(int cid) { creditDao.deleteCreditDebit(cid); } @Override public List<Credit> getAllCreditDebit() { return creditDao.getAllCreditDebit(); } }
package com.rojmat.daoImpl; import java.util.List; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.classic.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.rojmat.dao.CreditDao; import com.rojmat.entity.Credit; import com.rojmat.entity.Debit; @Repository public class CreditDaoImpl implements CreditDao{ @Autowired private SessionFactory sessionFactory; @Override public void addCreditDebit(Credit credit) { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { session.save(credit); tx.commit(); } catch(Exception e) { session.close(); tx.rollback(); e.printStackTrace(); } } @Override public void updateCreditDebit(Credit credit) { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { session.update(credit); tx.commit(); session.close(); } catch(Exception e) { session.close(); tx.rollback(); e.printStackTrace(); } } @Override public void deleteCreditDebit(int cid) { Credit credit = (Credit)sessionFactory.getCurrentSession().createQuery("from Credit as c LEFT JOIN FETCH c.debit where c.cid="+cid).uniqueResult(); List<Debit> debits = credit.getDebit(); sessionFactory.getCurrentSession().delete(credit); debits.forEach((debit) -> { sessionFactory.getCurrentSession().delete(debit); }); } @SuppressWarnings("unchecked") @Override public List<Credit> getAllCreditDebit() { List<Credit> credits = sessionFactory.getCurrentSession().createQuery("from Credit").list(); return credits; } }