Home > Software design >  Each time a page loads, how do I save the Auto Increment count in a database using Hibernate?
Each time a page loads, how do I save the Auto Increment count in a database using Hibernate?

Time:11-11

I have this Entity class, The table is being generated, but I'm unable to fill it with data.

@Entity
@Table(name = "app_form_order_Id")
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "orderId")
    private Integer orderId;


    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

}

Now, each time this loads function is invoked in the controller, we must increase the data in the database.

@RequestMapping(value = "/ajax/form/{formId}/load", method = RequestMethod.GET)
public @ResponseBody AjaxResponseBean loadForm(HttpSession session, @PathVariable Integer formId, @RequestBody AjaxRequestBean request) {
    .
    .
        if (!"designer".equals(request.getData().getMode())){
                Order order = new Order();
        }
    .
    .
}

This is the service class


public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderDao orderDao;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Order addOrder(Order orderId) {
        return orderDao.addOrder(orderId);
    }
}

Dao class


public class OrderDaoImpl implements OrderDao{

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Order addOrder(Order order) {
        sessionFactory.getCurrentSession().saveOrUpdate(order);
        return order;
    }

Can somebody help me figure out how to link it to the database?

table

CodePudding user response:

There are many mistakes you made here.

  1. You don't need to return Order back (DAO and service).
  2. In the controller you are creating a new Order object. That means it is an empty object.

Since orderId is auto incremented, here I edited it to add lastUpdate .

DAO class

public class OrderDaoImpl implements OrderDao{

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addOrder(Order order) {
        sessionFactory.getCurrentSession().saveOrUpdate(order);
       
    }

Service class

public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderDao orderDao;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addOrder(Order order) {
        orderDao.addOrder(order);
    }
}

Controller

@RequestMapping(value = "/ajax/form/{formId}/load", method = RequestMethod.GET)
public @ResponseBody AjaxResponseBean loadForm(HttpSession session, @PathVariable Integer formId, @RequestBody AjaxRequestBean request) {
if (!"designer".equals(request.getData().getMode())){
        Order order = new Order();
        order.setLastUpdate(new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()););
        orderServiceImpl.addOrder(order);
   }
}

Bean Class

@Entity
@Table(name = "app_form_order_Id")
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "orderId")
    private Integer orderId;
    
    @Column(name = "lastUpdate")
    private Date lastUpdate;


    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }
    
    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

}

This is roughly typed code. Please try it. 

  • Related