These are chat archives for hibernate/hibernate-orm

8th
Aug 2017
Al Grant
@bigalnz
Aug 08 2017 01:05
I have an episode_person table so perhaps it could be stored on this table? but what would the annotations look like?
Chris Cranford
@Naros
Aug 08 2017 14:27
@bigalnz I'd consider solving that using a tertiary entity association.
Essentially you have an EpisodePersonRole entity where you basically associate Episode <-> Person <-> Role together as a single unit.
You can then create relations to this entity from Person to get all episode roles and from Episode to get all Person entities and roles.
Your episode_person table most likely has the EPISODE_ID and PERSON_ID fields?
If so, you could reuse the table, binding the tertiary entity to it rather than the table being based on some association between Episode and Person.
And then you simply need to add the column for the role.
Al Grant
@bigalnz
Aug 08 2017 17:42
Hi Chris. That's what I thought. I will have to read in how to set it up. Where does annotation EpisodePersonRole go?
ryan2049
@ryan2049
Aug 08 2017 18:08
it would be its own @Entity
Chris Cranford
@Naros
Aug 08 2017 19:02
@bigalnz As @ryan2049 mentioned, you build a join-table relationship using a tertiary entity.
Al Grant
@bigalnz
Aug 08 2017 19:06
Ahhh, ok.
Will read the link now :smile:
Al Grant
@bigalnz
Aug 08 2017 19:16
So first thing I notice reading the book publisher example is he actually has a Entity for BookPublisher - whereas I just have a relationship defined (and a corresponding table)
Chris Cranford
@Naros
Aug 08 2017 19:17
That's right.
Al Grant
@bigalnz
Aug 08 2017 19:18
And my relationship is ManyToMany both sides, whereas he is OneToMany
Chris Cranford
@Naros
Aug 08 2017 19:18
If you want to store additional attributes as a part of a join between two entities (as in your case), you would do that with a tertiary entity that you use as the binding.
hence his use of BookPublisher to bind a Book and a Publisher beyond just PK<->FK relationships
Al Grant
@bigalnz
Aug 08 2017 19:19
right, so a tertiary entity (new term) is begining to sound like a entity that relates to a join table?
Chris Cranford
@Naros
Aug 08 2017 19:19
That's precisely what it is.
Al Grant
@bigalnz
Aug 08 2017 19:20
but has the advantage that its more powerful in that it can hold additional information
Chris Cranford
@Naros
Aug 08 2017 19:21
the main difference is that rather than the join-table being managed based on your many-to-many mapping you'll have to manage it more yourself.
e.g. create BookPublisher, call setBook( book ), call setPublisher( publisher ), call setWhateverYourExtraThingIs( extraStuff ); and then persist it.
Al Grant
@bigalnz
Aug 08 2017 19:21
so when persisting a Episode, I will need to look at the save methods, and probably modify them?
Chris Cranford
@Naros
Aug 08 2017 19:21
and making any necessary back references to Book and Publisher accordingly.
Where you add the association logic is really highly application-driven.
Al Grant
@bigalnz
Aug 08 2017 19:22
right. sounds like fun (not really)
Chris Cranford
@Naros
Aug 08 2017 19:22
depends on the use case
Al Grant
@bigalnz
Aug 08 2017 19:23
I have a EpisodeServiceImpl
which has a saveEpisode method
ryan2049
@ryan2049
Aug 08 2017 19:23
give it some real thought before creating a monster
Al Grant
@bigalnz
Aug 08 2017 19:23
ryan2049 : what do you mean?
Chris Cranford
@Naros
Aug 08 2017 19:24
remember how I somewhat encouraged you to think about cascade and avoid it unless it seemed absolutely necessary?
Al Grant
@bigalnz
Aug 08 2017 19:24
yip
Chris Cranford
@Naros
Aug 08 2017 19:24
there are use cases where cascade makes a lot of sense.
e.g. an order and its associated line items
but there are other cases where it honestly can make the business logic far more brittle.
where persisting each object separately through its own service and api is more flexible in the long-haul
this is your onion problem and you're now adding another layer to that peel.
ryan2049
@ryan2049
Aug 08 2017 19:25
think about ways to simplify these domain models, whatever you do at this level will affect your business logic
Chris Cranford
@Naros
Aug 08 2017 19:25
tl;dr: you may need to re-evaluate your entire save approach for Episodes and adjust
Al Grant
@bigalnz
Aug 08 2017 19:26
@Naros yeah reading the book publisher example I had already come to the conclusion that I was going to have to rip apart my save episode method
and indeed person and episode models
So everything in the BookPublisher table is inserted manually?
Chris Cranford
@Naros
Aug 08 2017 19:27
Your EpisodeService may expose something like assignEpisodeUser(Long episodeId, Long userId, Long roleId);
internally it looks up the Episode, the User, associates them with the given role. If it doesn't already exist and if so, it adjusts the role.
Al Grant
@bigalnz
Aug 08 2017 19:28
it will be Person rather than User but yes I follow
@ryan2049 How can I simplify the relationships? they are what they are
Chris Cranford
@Naros
Aug 08 2017 19:29
As far as whether BookPublisher is inserted manually really depends.
you could have List<BookPublisher> be cascaded from Book if you really wanted to do it that way.
but again i caution you on your cascade usage and only really apply it if it truly makes sense.
Al Grant
@bigalnz
Aug 08 2017 19:30
Right.
Is this an advanced topic?
Chris Cranford
@Naros
Aug 08 2017 19:30
I think what @ryan2049 was getting at was to really sit back and try to draw lines between your models.
Al Grant
@bigalnz
Aug 08 2017 19:31
hows your project going?
your work
Chris Cranford
@Naros
Aug 08 2017 19:31
Make sure you can clearly separate X from Y in such a way that they're more components you "glue" rather than necessarily high-coupled domain entities.
e.g. A Person/User is just that, represents some real life individual.
If you need to represent that real life individual in the context of multiple things, a tertiary entity comes in handy if you need "additional" relational stuff.
Al Grant
@bigalnz
Aug 08 2017 19:33
If I have a go at it and its not working (which lets be honest it wont) - can I email you the draft?
Chris Cranford
@Naros
Aug 08 2017 19:33
you can and i'll respond as I am able
Al Grant
@bigalnz
Aug 08 2017 19:33
At least for the annotations first
Thanks
Chris Cranford
@Naros
Aug 08 2017 19:33
or you can always ask here too in case. @ryan2049 is a good resource too.
Al Grant
@bigalnz
Aug 08 2017 19:33
I am so sad to break what I have so far as its working quite nicely
Chris Cranford
@Naros
Aug 08 2017 19:34
You're using version control, yes?
Al Grant
@bigalnz
Aug 08 2017 19:34
I dont think it is setup correctly - but I have local history
Chris Cranford
@Naros
Aug 08 2017 19:34
if so, branch and implement the changes. if they don't work out, you re-branch from master and try in another branch.
at least then you can always compare your local work to the master version should you need to see what you did previously or revert
Al Grant
@bigalnz
Aug 08 2017 19:35
oh i see - is that what a branch is for
Chris Cranford
@Naros
Aug 08 2017 19:35
branches are for a lot of various things
Al Grant
@bigalnz
Aug 08 2017 19:35
no but that just gave its use context in real world
Chris Cranford
@Naros
Aug 08 2017 19:36
one super common thing is to maintain multiple versions of a project. e.g. Hibernate 4.3, 5.0, 5.1, 5.2, 6.0, etc.
those are all branches and master tends to point to whatever the active one is.
for hibernate, thats still 5.2
Al Grant
@bigalnz
Aug 08 2017 19:36
ok
Chris Cranford
@Naros
Aug 08 2017 19:36
but its also good practice for implementing bugfixes, new features, etc.
that way you can separate your changes and integrate them as needed rather than imposing one depending on another.
Al Grant
@bigalnz
Aug 08 2017 19:37
I will get onto the github channel and try to get my VCS working again
yes, I want to have a working draft project, but also be able to fiddle with what I have done without breaking the working draft
its hard when people want to see whats been done and you have to explain its broken because your in the middle of working on something
Chris Cranford
@Naros
Aug 08 2017 19:38
then definitely setup VCS
branch and fiddle without breaking master
Al Grant
@bigalnz
Aug 08 2017 19:38
exactly
Chris Cranford
@Naros
Aug 08 2017 19:38
when your fiddle work works as you like it, merge it into master
Al Grant
@bigalnz
Aug 08 2017 19:38
ok
Chris Cranford
@Naros
Aug 08 2017 19:39
and that solves your last comment too.
Al Grant
@bigalnz
Aug 08 2017 19:39
exactly
Chris Cranford
@Naros
Aug 08 2017 19:39
someone wants to see the code work, you commit your changes to the branch, checkout master, clean & compile then run
ezpz
Al Grant
@bigalnz
Aug 08 2017 19:39
sweeeeet
how do you name branches?
Chris Cranford
@Naros
Aug 08 2017 19:40
assuming i am on master and I want to checkout a new branch:
git checkout -b [name of new branch]
that branch will have HEAD of master as the base point and any changes you make are based on that point in time.
as for naming convention, its really a matter of preference.
one thing we've started lately is anything that is going to be a super-long work in progress like branch, we've been using "wip/6.0" as an example.
something where I am fixing a bug reported by a JIRA, we tend to name them the JIRA issue key (e.g. HHH-12345)
but its really up to you, just be consistent.
Makes life easier downstream when you have all these branches & you decide to do a spring-cleaning on your repo
I also generally delete branches I have merged too
Al Grant
@bigalnz
Aug 08 2017 19:59
ok
ryan2049
@ryan2049
Aug 08 2017 20:03
github is your friend
Al Grant
@bigalnz
Aug 08 2017 20:03
yeah just trying to get it working with Intelij again now
Commit failed with error: 0 files committed, 45 files failed to commit: Commit working version 9/8/17 0 WIP process control pathspec 'java/com/example/planController.java' did not match any file(s) known to git.error: pathspec 'java/com/example/formController.java' did not match any file(s) known to git.error: pathspec 'java/com/example/taskController.java' did not match any file(s) known to git. (2 minutes ago)
ryan2049
@ryan2049
Aug 08 2017 20:05
don't leave the first letter of your classes lowercase
that's not standard for java development
makes my eyes hurt
Al Grant
@bigalnz
Aug 08 2017 20:05
ok - but thats not why it didnt commit surely
Chris Cranford
@Naros
Aug 08 2017 20:30
that's a strange error, how are you committing?
Al Grant
@bigalnz
Aug 08 2017 21:15
ok - got the VCS sorted and have created a branch :-)
will have a crack at the tertiary setup later - its really hard in the office with all the distractions
Chris Cranford
@Naros
Aug 08 2017 21:17
lol
Al Grant
@bigalnz
Aug 08 2017 21:18
seriously you might do this in your sleep - I need to concentrate
Chris Cranford
@Naros
Aug 08 2017 21:18
Most tend to, especially if the task at hand is something relatably complicated
Al Grant
@bigalnz
Aug 08 2017 21:19
every 5 minutes someone will come and interrupt me