Generate Persistence Mapping by Database Schema - Detail settings for Entity Class

Answered
 
I’ve been using Spring Tool Suite for all my previous projects quite long, and now I am trying to utilize IntelliJ IDE for my next project. 
 
Before I decide to purchase IntelliJ IDEA Ultimate License, I wanted to check out if it would meet all the requirements that I need. 
 
I have been trying to find out some functions that I used to use in Spring Tool Suite. 
 
But I cannot find them after a long search on Google.
 
Therefore, I decide to post this to get some help from you.   
 
I believe it is either IntelliJ IDE may not have these functions or they are somewhere that I can't find.
 
I will ask a couple questions as I go through step by step.
 
  • First, I created test project using Spring Boot
 
  • I right clicked the project and “Add Framework Support” and select “JavaEE Persistence(2.0)” and chose some settings and clicked "OK"

 

 

  • After this step, it will look like the screenshot as below. View —> Tool Windows —> Database & Persistence, and I connected to my local DB (MySQL). 
      
  
 
 
 
  • Under the “Persistence” tab at the right side, I right clicked "demo" project and go to “Generated Persistence Mapping” and go to click the “By Database Schema”. This will pop up the window like the screenshot below.
 
 
  • Going through this step, I have some couple questions to ask. 
I chose and selected as the screenshot below to create a new Entity Class.
 
 
 
 
By clicking "OK", it will generate a new class called "UserEntity" with below codes.
 
UserEntity

@Entity

@Table(name = "user", schema = "test_db", catalog = "")

public class UserEntity {

     private int id;

     private String confirmationToken;

     private String email;

     private Boolean enabled;

     private String firstName;

     private String lastName;

     private String password;




     @Id

     @Column(name = "id")

     public int getId() {

          return id;

     }




     public void setId(int id) {

          this.id = id;

     }




     @Basic

     @Column(name = "confirmation_token")

     public String getConfirmationToken() {

          return confirmationToken;

     }




     public void setConfirmationToken(String confirmationToken) {

          this.confirmationToken = confirmationToken;

     }




     @Basic

     @Column(name = "email")

     public String getEmail() {

          return email;

     }




     public void setEmail(String email) {

          this.email = email;

     }




     @Basic

     @Column(name = "enabled")

     public Boolean getEnabled() {

          return enabled;

     }




     public void setEnabled(Boolean enabled) {

          this.enabled = enabled;

     }




     @Basic

     @Column(name = "first_name")

     public String getFirstName() {

          return firstName;

     }




     public void setFirstName(String firstName) {

          this.firstName = firstName;

     }




     @Basic

     @Column(name = "last_name")

     public String getLastName() {

          return lastName;

     }




     public void setLastName(String lastName) {

          this.lastName = lastName;

     }




     @Basic

     @Column(name = "password")

     public String getPassword() {

          return password;

     }




     public void setPassword(String password) {

          this.password = password;

     }




     @Override

     public boolean equals(Object o) {

          if (this == o) return true;

          if (o == null || getClass() != o.getClass()) return false;




          UserEntity that = (UserEntity) o;




          if (id != that.id) return false;

          if (confirmationToken != null ? !confirmationToken.equals(that.confirmationToken) : that.confirmationToken != null)

               return false;

          if (email != null ? !email.equals(that.email) : that.email != null) return false;

          if (enabled != null ? !enabled.equals(that.enabled) : that.enabled != null) return false;

          if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) return false;

          if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) return false;

          if (password != null ? !password.equals(that.password) : that.password != null) return false;




          return true;

     }




     @Override

     public int hashCode() {

          int result = id;

          result = 31 * result + (confirmationToken != null ? confirmationToken.hashCode() : 0);

          result = 31 * result + (email != null ? email.hashCode() : 0);

          result = 31 * result + (enabled != null ? enabled.hashCode() : 0);

          result = 31 * result + (firstName != null ? firstName.hashCode() : 0);

          result = 31 * result + (lastName != null ? lastName.hashCode() : 0);

          result = 31 * result + (password != null ? password.hashCode() : 0);

          return result;

     }

}
 
 
Q1. I created user table with id as auto_increment. 
 


 
 
But, as you can see the generated codes inside the UserEntity class, it does not have @GeneratedValue.
 
@GeneratedValue(strategy=GenerationType.IDENTITY)
 
Is there somewhere to select this to be generated?
 
 
 
Q2.  I know by default, Entity is there inside the "Entity suffix".
        I thought If I leave it blank as to generate the new class name "User", 
        it would not generate @Table annotation.  Is that normal behavior?
 
 

 
 User

 

 @Entity

public class User {

     private int id;

     private String confirmationToken;

     private String email;

     private Boolean enabled;

     private String firstName;

     private String lastName;

     private String password;




     @Id

     @Column(name = "id")

     public int getId() {

          return id;

     }




     public void setId(int id) {

          this.id = id;

     }




     @Basic

     @Column(name = "confirmation_token")

     public String getConfirmationToken() {

          return confirmationToken;

     }




     public void setConfirmationToken(String confirmationToken) {

          this.confirmationToken = confirmationToken;

     }




     @Basic

     @Column(name = "email")

     public String getEmail() {

          return email;

     }




     public void setEmail(String email) {

          this.email = email;

     }




     @Basic

     @Column(name = "enabled")

     public Boolean getEnabled() {

          return enabled;

     }




     public void setEnabled(Boolean enabled) {

          this.enabled = enabled;

     }




     @Basic

     @Column(name = "first_name")

     public String getFirstName() {

          return firstName;

     }




     public void setFirstName(String firstName) {

          this.firstName = firstName;

     }




     @Basic

     @Column(name = "last_name")

     public String getLastName() {

          return lastName;

     }




     public void setLastName(String lastName) {

          this.lastName = lastName;

     }




     @Basic

     @Column(name = "password")

     public String getPassword() {

          return password;

     }




     public void setPassword(String password) {

          this.password = password;

     }




     @Override

     public boolean equals(Object o) {

          if (this == o) return true;

          if (o == null || getClass() != o.getClass()) return false;




          User user = (User) o;




          if (id != user.id) return false;

          if (confirmationToken != null ? !confirmationToken.equals(user.confirmationToken) : user.confirmationToken != null)

               return false;

          if (email != null ? !email.equals(user.email) : user.email != null) return false;

          if (enabled != null ? !enabled.equals(user.enabled) : user.enabled != null) return false;

          if (firstName != null ? !firstName.equals(user.firstName) : user.firstName != null) return false;

          if (lastName != null ? !lastName.equals(user.lastName) : user.lastName != null) return false;

          if (password != null ? !password.equals(user.password) : user.password != null) return false;




          return true;

     }




     @Override

     public int hashCode() {

          int result = id;

          result = 31 * result + (confirmationToken != null ? confirmationToken.hashCode() : 0);

          result = 31 * result + (email != null ? email.hashCode() : 0);

          result = 31 * result + (enabled != null ? enabled.hashCode() : 0);

          result = 31 * result + (firstName != null ? firstName.hashCode() : 0);

          result = 31 * result + (lastName != null ? lastName.hashCode() : 0);

          result = 31 * result + (password != null ? password.hashCode() : 0);

          return result;

     }

}
 
 
 
Q3. Are there any selectable settings that I can select IN DETAIL for @Column annotation when generating persistence mapping by database schema? I don't find anywhere to select those in detail.
 
 


 

 

 

 

 

 

 

 

 

 

 

7 comments
Comment actions Permalink

>Q1. I created user table with id as auto_increment. But, as you can see the generated codes inside the UserEntity class, it does not have @GeneratedValue.

Currently it is not generated: https://youtrack.jetbrains.com/issue/IDEA-97873

 

 

>Q2.  I know by default, Entity is there inside the "Entity suffix". I thought If I leave it blank as to generate the new class name "User",  it would not generate @Table annotation.  Is that normal behavior?


See 2.2.1.1. Defining the table section in hiberante documentation:

>@Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity. So it is expected behaviour.

 

 

>Q3. Are there any selectable settings that I can select IN DETAIL for @Column annotation when generating persistence mapping by database schema? I don't find anywhere to select those in detail.

Currently it is not possible to customize mapping generation, please vote for this request in our tracker: https://youtrack.jetbrains.com/issue/IDEA-76638

0
Comment actions Permalink

@Andrey Dernov

 

Thank you for your feedback. 

Do you have any idea if IntelliJ IDE Developers are currently working on this issue now ?

It seems like not a lot of developers feel uncomfortable using mapping generation or not a lot of developers are using this. 

because the issue (https://youtrack.jetbrains.com/issue/IDEA-76638 ) is created by Georg Hendlmeier 08 Nov 2011 17:22.

 

0
Comment actions Permalink

Andrey Dernov

>@Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity. So it is expected behaviour.

We just found that this is misleading behavior in case I want entity named same as a table, but table is in different schema/catalog cause in this case no @Table annotation will not be generated where schema and catalog are specified. Expected behavior: @Table annotation is generated with schema/catalog details specified(no "name" property specified).

0
Comment actions Permalink

@Pavel Sakun

Do I understand correctly: the problem is when you are generating entities for different schemas the schema name is not generated? Could you provide a screenshots with your steps? Thank you.

0
Comment actions Permalink

Andrey Dernov yes, if we don't specify entity suffix then @Table annotation is not generated and this means that default schema is assumed.

Here is what we do:

And here is what we get:

As our tables are in different schemas I would assume that it should still generate @Table(schema="schemaName") even if table name is the same as entity class name.

One more issue we found:

there is layout issue which causes target table name combobox be of a fixed size even if dialog window is stretched horizontally. This makes it impossible to select correct table as you cannot see it's name if it's long enough and there even no hint for combobox's values which you can use to see full table name.

0
Comment actions Permalink

This option appears to be not working for H2 in memory database. I think tool need improvement to support.

0

Please sign in to leave a comment.