IntelliJ ER Diagram Relationship Dialogs and the Code They Create

I thought it would be interesting to walk through a few dialogs in IntelliJ's Persistence View tools and see the relationship wiring code they create. Sounds like fun, doesn't it? Well let's go!

First we should quickly review the database schema for my Spring-Data app on GitHub where this is found. I didn't create the schema and I don't happen to like it, but it will serve us well to better understand the IntelliJ ER Diagram dialogs and the code they create.

We have our primary contact table with a one-to-many relationship to contact_tel_detail which lists mobile and home phones for each contact if available. We also have a many-to-many relationship between contact and hobby via contact_hobby_detail as our join table.

Below we see our ER Diagram in IntelliJ which displays Entities very similar in design to our database schema, but with no contact_hobby_detail object, as it functions in our shema as a join table only.

Now, onto our relationship dialog forms. Here we see an “Edit Relationship” dialog for the one-to-many relationship between contact and contact_tel_detail.

Now let's look at the relationship wiring code this ER Diagram Dialog produces. First our ContactEntity class. It's important to consider the inverse perspective of the properties. That is, properties like Cascade Type, Owner, etc shown in the above dialog apply to the Attribute/Joined Entity on the other side of the relationship, not the Class Entity itself.

private Set<ContactTelDetailEntity> contactTelDetailEntities;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "contactEntity")
public Set<ContactTelDetailEntity> getContactTelDetailEntities() {
	return contactTelDetailEntities;
}

public void setContactTelDetailEntities(Set<ContactTelDetailEntity> contactTelDetailEntities) {
	this.contactTelDetailEntities = contactTelDetailEntities;
}

You'll notice the @JoinColumn info for the relationship is in the ContactTelDetailEntity @ManyToOne description.

private ContactEntity contactEntity;

@ManyToOne
@JoinColumn(name = "contact_id", referencedColumnName = "id", nullable = false)
public ContactEntity getContactEntity() {
	return contactEntity;
}

public void setContactEntity(ContactEntity contactEntity) {
	this.contactEntity = contactEntity;
}

Creating Relationships with the Database Schema Mapper

For creating relationships with a bit more granularity we have the option of using the Database Schema Mapper. Funny, I originally thought you had a one-time shot to use the Database Schema Mapper to generate models and relationship and that you were limited to using the ER Diagram afterward. I was happily surprised to discover that you can fire up the Database Schema Mapper to create individual relationships and entities anytime with no loss of your existing Persistence Unit objects.

Relationships created with the database schema dialog provide a few more database-oriented options like below where we create a @ManyToMany relationship between Contact and Hobby using a join table, contact_hobby_detail. There is no option of using Join Tables creating relationships in the ER Diagram Mapper.

Let's look at the code we generated in the ContactEntity and HobbyEntity classes using the above dialog box from the Database Schema Mapper. First, ContactEntity.

private Set<HobbyEntity> hobbyEntities;

@ManyToMany
@JoinTable(name = "contact_hobby_detail", catalog = "dev_hibernate", schema = "", 
	joinColumns = @JoinColumn(name = "contact_id", referencedColumnName = "id", nullable = false), 
	inverseJoinColumns = @JoinColumn(name = "hobby_id", referencedColumnName = "hobby_id", nullable = false))

public Set<HobbyEntity> getHobbyEntities() {
	return hobbyEntities;
}

public void setHobbyEntities(Set<HobbyEntity> hobbyEntities) {
	this.hobbyEntities = hobbyEntities;
}

mappedBy=”hobbyEntities” was created in HobbyEntity for the relationship referring to the name of the @ManyToMany relationship attribute in ContactEntity.

private Set<ContactEntity> contactEntities;
 
@ManyToMany(mappedBy = "hobbyEntities")
public Set<ContactEntity> getContactEntities() {
	return contactEntities;
}

public void setContactEntities(Set<ContactEntity> contactEntities) {
	this.contactEntities = contactEntities;
}

Here's the Database Shema Mapper dialog for good GUI completeness.

More Like This Post