Attempting achieve low coupling between JPA entities












1















One issue I'm running into is a good way to map One-To-Many relationships with JPA / Hibernate, without sacrificing SOLID principles along the way. Here's an example of this problem from a current project I'm working on:



Given the following code which defines a unidirectional One-To-Many relationship:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name = "fk_hasContacts")
private Set<Contact> contacts;}


My understanding is that this creates an "fk_hasContacts" column in the "contacts" table, without the "Contact" class knowing or caring which object it is being referenced from. Also, notice how "User" implements the "hasContacts" interface, which creates a decoupling effect between the two entities. If tomorrow I want to add another class; say "BusinessEntity" which also has contacts, there's nothing that needs to be changed within the current code. However, after reading up on the topic in seems that this approach is inefficient from a database performance perspective.




The best way to map a @OneToMany association is to rely on the @ManyToOne side to propagate all entity state changes - As expounded upon here.




Seems to be the prevailing wisdom. Were I to engineer it that way, the "User" class would now look like this:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Set<Contact> contacts;


And the (previously decoupled) "Contact" class would now have to look like this:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private User user;


The key problem is that it seems from here that JPA doesn't support defining an interface as an entity attribute . So this code:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private HasContacts hasContacts;//some class which implements the "HasUser" interface


Isn't an option. Seems one would have to chose between SOLID OOP, and valid ORM entities. After a lot of reading I still don't know whether the hit to database performance is bad enough to justify writing what amounts to tightly coupled, rigid and ultimately bad code.



Are there any workarounds / solutions to this seeming contradiction in design principles?










share|improve this question


















  • 1





    If you don't want contact to know about the user(s), then use a OneToMany (or ManyToMany) unidirectional association, with the default mapping for such associations: a join table.

    – JB Nizet
    Nov 14 '18 at 17:09


















1















One issue I'm running into is a good way to map One-To-Many relationships with JPA / Hibernate, without sacrificing SOLID principles along the way. Here's an example of this problem from a current project I'm working on:



Given the following code which defines a unidirectional One-To-Many relationship:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name = "fk_hasContacts")
private Set<Contact> contacts;}


My understanding is that this creates an "fk_hasContacts" column in the "contacts" table, without the "Contact" class knowing or caring which object it is being referenced from. Also, notice how "User" implements the "hasContacts" interface, which creates a decoupling effect between the two entities. If tomorrow I want to add another class; say "BusinessEntity" which also has contacts, there's nothing that needs to be changed within the current code. However, after reading up on the topic in seems that this approach is inefficient from a database performance perspective.




The best way to map a @OneToMany association is to rely on the @ManyToOne side to propagate all entity state changes - As expounded upon here.




Seems to be the prevailing wisdom. Were I to engineer it that way, the "User" class would now look like this:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Set<Contact> contacts;


And the (previously decoupled) "Contact" class would now have to look like this:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private User user;


The key problem is that it seems from here that JPA doesn't support defining an interface as an entity attribute . So this code:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private HasContacts hasContacts;//some class which implements the "HasUser" interface


Isn't an option. Seems one would have to chose between SOLID OOP, and valid ORM entities. After a lot of reading I still don't know whether the hit to database performance is bad enough to justify writing what amounts to tightly coupled, rigid and ultimately bad code.



Are there any workarounds / solutions to this seeming contradiction in design principles?










share|improve this question


















  • 1





    If you don't want contact to know about the user(s), then use a OneToMany (or ManyToMany) unidirectional association, with the default mapping for such associations: a join table.

    – JB Nizet
    Nov 14 '18 at 17:09
















1












1








1








One issue I'm running into is a good way to map One-To-Many relationships with JPA / Hibernate, without sacrificing SOLID principles along the way. Here's an example of this problem from a current project I'm working on:



Given the following code which defines a unidirectional One-To-Many relationship:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name = "fk_hasContacts")
private Set<Contact> contacts;}


My understanding is that this creates an "fk_hasContacts" column in the "contacts" table, without the "Contact" class knowing or caring which object it is being referenced from. Also, notice how "User" implements the "hasContacts" interface, which creates a decoupling effect between the two entities. If tomorrow I want to add another class; say "BusinessEntity" which also has contacts, there's nothing that needs to be changed within the current code. However, after reading up on the topic in seems that this approach is inefficient from a database performance perspective.




The best way to map a @OneToMany association is to rely on the @ManyToOne side to propagate all entity state changes - As expounded upon here.




Seems to be the prevailing wisdom. Were I to engineer it that way, the "User" class would now look like this:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Set<Contact> contacts;


And the (previously decoupled) "Contact" class would now have to look like this:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private User user;


The key problem is that it seems from here that JPA doesn't support defining an interface as an entity attribute . So this code:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private HasContacts hasContacts;//some class which implements the "HasUser" interface


Isn't an option. Seems one would have to chose between SOLID OOP, and valid ORM entities. After a lot of reading I still don't know whether the hit to database performance is bad enough to justify writing what amounts to tightly coupled, rigid and ultimately bad code.



Are there any workarounds / solutions to this seeming contradiction in design principles?










share|improve this question














One issue I'm running into is a good way to map One-To-Many relationships with JPA / Hibernate, without sacrificing SOLID principles along the way. Here's an example of this problem from a current project I'm working on:



Given the following code which defines a unidirectional One-To-Many relationship:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name = "fk_hasContacts")
private Set<Contact> contacts;}


My understanding is that this creates an "fk_hasContacts" column in the "contacts" table, without the "Contact" class knowing or caring which object it is being referenced from. Also, notice how "User" implements the "hasContacts" interface, which creates a decoupling effect between the two entities. If tomorrow I want to add another class; say "BusinessEntity" which also has contacts, there's nothing that needs to be changed within the current code. However, after reading up on the topic in seems that this approach is inefficient from a database performance perspective.




The best way to map a @OneToMany association is to rely on the @ManyToOne side to propagate all entity state changes - As expounded upon here.




Seems to be the prevailing wisdom. Were I to engineer it that way, the "User" class would now look like this:



@Entity
@Table(name = "users")
public class User extends AggregateEntity<User> implements HasRoles, HasContactInfo, HasContacts, HasDeals {

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Set<Contact> contacts;


And the (previously decoupled) "Contact" class would now have to look like this:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private User user;


The key problem is that it seems from here that JPA doesn't support defining an interface as an entity attribute . So this code:



@Entity
@Table(name = "contacts")
public class Contact extends StandardEntity<Contact> {

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "fk_user")
private HasContacts hasContacts;//some class which implements the "HasUser" interface


Isn't an option. Seems one would have to chose between SOLID OOP, and valid ORM entities. After a lot of reading I still don't know whether the hit to database performance is bad enough to justify writing what amounts to tightly coupled, rigid and ultimately bad code.



Are there any workarounds / solutions to this seeming contradiction in design principles?







oop jpa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 16:57









David FDavid F

61




61








  • 1





    If you don't want contact to know about the user(s), then use a OneToMany (or ManyToMany) unidirectional association, with the default mapping for such associations: a join table.

    – JB Nizet
    Nov 14 '18 at 17:09
















  • 1





    If you don't want contact to know about the user(s), then use a OneToMany (or ManyToMany) unidirectional association, with the default mapping for such associations: a join table.

    – JB Nizet
    Nov 14 '18 at 17:09










1




1





If you don't want contact to know about the user(s), then use a OneToMany (or ManyToMany) unidirectional association, with the default mapping for such associations: a join table.

– JB Nizet
Nov 14 '18 at 17:09







If you don't want contact to know about the user(s), then use a OneToMany (or ManyToMany) unidirectional association, with the default mapping for such associations: a join table.

– JB Nizet
Nov 14 '18 at 17:09














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305240%2fattempting-achieve-low-coupling-between-jpa-entities%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305240%2fattempting-achieve-low-coupling-between-jpa-entities%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings