2

I have 3 pojo's: Customer, Product and Transcation.

Customer.java:

@Id
@GeneratedValue
@Column
private Long id;
@Column
private String name;

// getters and setters 

Product.java:

@Id
@GeneratedValue
@Column
private Long id;
@Column
private String productName;

// getters and setters 

Transcation.java:

@Id
@GeneratedValue
@Column
private Long id;
@JoinColumn
@OneToOne     // is this relation mapping correct
private Customer customer;
@JoinColumn
@OneToMany    // is this relation mapping correct
private List<Product> product;

// getters and setters ... and my idea is one transcation multiple products for one customer at a time

My index.jsp input form:

<form method="post" action="${pageContext.request.contextPath }/">
    <div>
        <label>User:</label> 
        <select name="customer">
            <option value="">Select Customer</option>
            <c:forEach var="c" items="${ cList }">
                <option value="${ c.id }">${ c.name }</option>
            </c:forEach>
        </select>
    </div><br>
    <div>
        <label>Hobby:</label> 
        <select name="product" multiple>
            <!-- <option value="">Select Items</option> -->
            <c:forEach var="p" items="${ pList }">
                <option value = "${ p.id }">${ p.productName }</option>
            </c:forEach>
        </select>
    </div>
    <br><br>
    <input type="submit" value="Send">
</form>

Here is the controller:

@Controller
@RequestMapping(value = "/")
public class HomeController {

@Autowired
private CustomerDao customerDao;
@Autowired
private ProductDao productDao;
@Autowired
private TranscationDao transcationDao;  

    @RequestMapping(method = RequestMethod.GET)
    public String home( Model model) {
        model.addAttribute("cList", customerDao.getAllCustomer());
        model.addAttribute("pList", productDao.getAllProduct());
        return "index";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String postHome(Model model, @ModelAttribute Transcation t){
        transcationDao.addTranscation(t);
        return "home";
    }

}

Here is my TranscationDaoImpl:

@Override
public void addTranscation(Transcation t) {
    session = sessionFactory.openSession();
    session.save(t);
    session.close();
}

I am having confusion on how to insert multiple product and a customer data into a transaction table in database.

What sort of modifications i have to make in my code in order to get my transaction function adding a customer_id as well as multiple product_ids in transaction table.

Here in transaction i have customer_id from customer table and product_id from product table.

What is need is to create a database record of every transaction made by a customer along with the product list purchased.

Space
  • 213
  • 4
  • 16
  • Consider this:https://stackoverflow.com/questions/20458401/how-to-insert-multiple-rows-into-database-using-hibernate –  Aug 17 '17 at 07:29

0 Answers0