Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "product")
- public class ProductEntity{
- ....
- private long productId;
- private List<StockEntity> stockEntityList;
- ....
- @Id
- @Column(name="productId")
- public long getProductId(){
- return this.productId;
- }
- @OneToMany(mappedBy="productEntity", fetch= FetchType.EAGER
- cascade={CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE},
- orphanRemoval = true)
- public List<StockEntity> getStockEntityList(){
- return this.stockEnityList;
- }
- public void setStockEntityList(List<StockEntity> stockEntityList){...}
- ....
- }
- @Entity
- @Table(name = "stock")
- public class StockEntity{
- ...
- private ProductEntity productEntity;
- private long startTime;
- ...
- @ManyToOne(fetch=FetchType.LAZY)
- @JoinColumn(name="productId")
- public ProductEntity getProductEntity(){
- return this.producntEntity;
- }
- @Id
- @Column(name="startTime")
- public long getStartTime(){
- return this.startTime;
- }
- ....
- }
- public void consumeStockQuantity(long productId, long startTime, int count){
- Session session = HBSession.getSession();
- Transaction tx = session.beginTransaction();
- ProductEntity productEntity = session.get(productId, ProductEntity.class);
- for(StockEntity stockEntity: productEntity.getStockEntityList()){
- if(stockEntity.getStartTime() == startTime){
- stockEntity.setQuantity(stockEntity.getQuantity-count);
- }
- }
- try{
- session.update(productEntity);
- tx.commit();
- }catch(Exception e){
- e.printStackTrace();
- tx.rollback();
- }finally{
- session.close();
- }
- }
- Hibernate:
- update
- test.stock
- set
- marketPrice=?,
- productId=?,
- purchasePrice=?,
- quantity=?,
- sellPrice=?
- where
- startTime=?
- HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [FLOAT] - [1.2]
- HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [1075]
- HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [FLOAT] - [0.01]
- HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [4] as [BIGINT] - [1000]
- HibernateLog --> 15:35:27 TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [5] as [FLOAT] - [0.01]
- 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