Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. @Entity
  2. @Table(name = "product")
  3. public class ProductEntity{
  4. ....
  5. private long productId;
  6. private List<StockEntity> stockEntityList;
  7. ....
  8.  
  9. @Id
  10. @Column(name="productId")
  11. public long getProductId(){
  12. return this.productId;
  13. }
  14.  
  15. @OneToMany(mappedBy="productEntity", fetch= FetchType.EAGER
  16. cascade={CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE},
  17. orphanRemoval = true)
  18. public List<StockEntity> getStockEntityList(){
  19. return this.stockEnityList;
  20. }
  21.  
  22. public void setStockEntityList(List<StockEntity> stockEntityList){...}
  23. ....
  24. }
  25.  
  26. @Entity
  27. @Table(name = "stock")
  28. public class StockEntity{
  29. ...
  30. private ProductEntity productEntity;
  31. private long startTime;
  32. ...
  33. @ManyToOne(fetch=FetchType.LAZY)
  34. @JoinColumn(name="productId")
  35. public ProductEntity getProductEntity(){
  36. return this.producntEntity;
  37. }
  38.  
  39. @Id
  40. @Column(name="startTime")
  41. public long getStartTime(){
  42. return this.startTime;
  43. }
  44.  
  45. ....
  46. }
  47.  
  48. public void consumeStockQuantity(long productId, long startTime, int count){
  49. Session session = HBSession.getSession();
  50. Transaction tx = session.beginTransaction();
  51. ProductEntity productEntity = session.get(productId, ProductEntity.class);
  52. for(StockEntity stockEntity: productEntity.getStockEntityList()){
  53. if(stockEntity.getStartTime() == startTime){
  54. stockEntity.setQuantity(stockEntity.getQuantity-count);
  55. }
  56. }
  57. try{
  58. session.update(productEntity);
  59. tx.commit();
  60. }catch(Exception e){
  61. e.printStackTrace();
  62. tx.rollback();
  63. }finally{
  64. session.close();
  65. }
  66. }
  67.  
  68. Hibernate:
  69. update
  70. test.stock
  71. set
  72. marketPrice=?,
  73. productId=?,
  74. purchasePrice=?,
  75. quantity=?,
  76. sellPrice=?
  77. where
  78. startTime=?
  79. HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [FLOAT] - [1.2]
  80. HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [1075]
  81. HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [FLOAT] - [0.01]
  82. HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [4] as [BIGINT] - [1000]
  83. HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [5] as [FLOAT] - [0.01]
  84. HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [BIGINT] - [1438175312412]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement