5

I have previously used Hibernate and now I am trying to understand JDBC. I have done loads of research on Spring JDBC but still I could not understand how to create relationships between objects.

Assume I have a Product:

public class Product {
private Long id;
private String nam;
private Customer customer;
//constructor, getters and setters.
}

and a Customer:

public class Customer {
private Long id;
private String name;
private List<Product> products = new ArrayList<Product>();
//constructor, getters and setters
}

The relationship between Customer and Product is @OneToMany. How to correctly save the product and customer objects in the db using SpringJDBC? Thank you

asked Nov 1, 2014 at 11:02
6
  • You don't. You use a JPA framework. Like Hibernate. Commented Nov 1, 2014 at 11:09
  • Is there anyway to do this using Spring JDBC? Commented Nov 1, 2014 at 11:17
  • Is there any way to cross the Atlantic in a car? Yes, sure - built a boat and put the car on it. Or you could just use a boat. Commented Nov 1, 2014 at 11:17
  • Complex enterprise are not built using ORM. That's why I am trying to 'build the boat and cross the Atlantic' Commented Nov 1, 2014 at 11:19
  • Yes. They are. ORM is used in almost of complex enterprise Java applications you can buy. If you don't want to use ORM then you will obviously, well, not get ORM. Commented Nov 1, 2014 at 11:21

3 Answers 3

4

It make a lot of sense in quite a few cases to not use a full blown ORM but rely on lower level of abstraction, like Spring JDBCTemplate and RowMapper. iBatis comes to mind as well. And that make sense even in large enterprise solutions.

If you leave the full blown ORM world, you will have to do additional work yourself. For example, you can write an SQL query with a join, returning all customer fields and all products of that customer, and iterate through it to map all that to Java object. In quite a few cases, the code can be as clean as what you would have with an ORM.

Writing all that data is more messy, especially if you need to optimize for stuff that has not been dirtied.

Best use case I can think of is batch processing, where control over data access becomes more important and higher level of abstraction do not necessarily make you more productive.

answered Nov 1, 2014 at 12:45
Sign up to request clarification or add additional context in comments.

Comments

1

If you are willing to consider something other than spring or hibernate, sormula can do what you describe. See the one to many example. If you name the foreign key on the many side the same as the primary key on the one side, then you don't need any annotations.

To store a reference to one-side object (Customer) in the many-side objects (Products), you can use the OneToManyCascade#foreignKeyReferenceField. For examples, search the tests in the project for "foreignKeyReferenceField".

answered Nov 4, 2014 at 21:13

Comments

0

If I understand your question correctly, if think if you are not using ORM you would have to do this manually. In your DAO class for Customer it would first have to persist all the products.

An alternative might be to create a Stored Procedure on the database and have that sort out the correct persistence.
This can be handled very nicely by Spring JDBC, the downside is you know have to manage Java and Stored Procedures. In your case it might still be two Stored Procedures.

There is also the possibility that QueryDSL and Jooq, thought I haven't had a chance to have a good look at them.

I personally like the Stored Procedure solution, for me the additional over head is worth it, I know others disagree, but I just don't like/buy the ORM deal.

answered Nov 1, 2014 at 12:05

2 Comments

No reason you cannot use stored procedures with ORM.
Thanks Boris, I am not sure I knew that, I will have a read it might come in useful :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.