• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Hibernate shenanigans  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all.

I am doing all the DB part of my current project and in order to avoid boilerplate I think I have created a monster.

Let's say the DB has two tables which have a m2m relationship, call them customer and product for simplicity. Now, in order to avoid opening and closing sessions and transactions for every single type of transaction, I have made a new class called DatabaseTransactions and an Enum class with all the types of transactions.
Now, depending on the type of transaction there will be a different number of arguments that I need to pass in order to successfully complete it, like I can only want to update the details of a customer, or of a product. Maybe I want to add only a customer and no products, maybe I want to only update a product a specific customer has bought and so on. This requires a lot of methods each one opening and closing sessions and all that stuff.

So what I did was pass the transaction type to the DatabaseTransactions class constructor along with a variable number of arguments (varargs). I then call the one and only method that does transactions that I named execTransaction. Inside it I have a switch case that based on the transactionType will instantiate the relevant classes and just know what to do.

In short, this has created a lot of code that, although not redundant, is much more than if I opted to just repeat the few lines for each method I need to update the DB. Plus it seems like a nightmare to maintain.

I was thinking there has got to be a better solution for this as it seems something a lot of devs have most likely run into.

Let me know what you think and thanks in advance.
 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is lol, the class you created sounds like some kind of repository...
I'm also not sure, but I think a customer/product relationship is considered oneToMany
In what are you using hibernate?
Cause I would suggest you use JPA-Hibernate , it will make your life more easy
The whole class you have created, you can drop.
Your relationship you map with:

By mapping it as CascadeType.All you only have to save one object for the other to be persisted automaticly, so if you mapped your class by customer and you save a customer the linked product get's also saved.
you then only have to write a single repository:

Just extending the Jparepository gives you all basic crud operations, if you wan't something more specific you add methods to the repository, for example let's say your customer class has two instance variables on which you want to find a customer, then you would right something like:

You don't have to write an implementation for this repository, JPA creates a query based on the instancevariables of your customerClass. There's lot's you can do with jpa, limit the results, order them, to mutch to explain, but if you have a descent idea that gives you suggestion on how to name your methods, it should be pretty straightforward
 
Amadeus Pan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel! Thanks for the  reply.

In my case it is many to many. I need to know exactly how many products have been bought by how many customers. The products are actually subscriptions in this case.

And thanks for the link, it solved my problem

Daniel Demesmaecker wrote:There is lol, the class you created sounds like some kind of repository...
I'm also not sure, but I think a customer/product relationship is considered oneToMany
In what are you using hibernate?
Cause I would suggest you use JPA-Hibernate , it will make your life more easy
The whole class you have created, you can drop.
Your relationship you map with:

By mapping it as CascadeType.All you only have to save one object for the other to be persisted automaticly, so if you mapped your class by customer and you save a customer the linked product get's also saved.
you then only have to write a single repository:

Just extending the Jparepository gives you all basic crud operations, if you wan't something more specific you add methods to the repository, for example let's say your customer class has two instance variables on which you want to find a customer, then you would right something like:

You don't have to write an implementation for this repository, JPA creates a query based on the instancevariables of your customerClass. There's lot's you can do with jpa, limit the results, order them, to mutch to explain, but if you have a descent idea that gives you suggestion on how to name your methods, it should be pretty straightforward

 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No prob, happy to help
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amadeus Pan wrote:. . . the DB has two tables which have a m2m relationship . . .

Can you actually write nn relations in your database? And please avoid writing “2” when you mean “to”.
 
Bartender
Posts: 20568
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Campbell is objecting to your description not actually asking literally if you can define a schema that way. I have no problem understanding "m2m" as "many-to-many", but the generally accepted formal notation is n..n. although I think I've seen "*..*" as well.

While there are actual Unix utilities that exploit the fact that "2" and "to" are pronounced identically in English (mostly conversion apps), we do have an international community here and perhaps some might first see "m2m" and "em zwei em", for example. So humor him.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!