1

I have started a project in Spring and was successfully able to run a sample application on Spring communicating with one table in MySQL database.

Now, for learning purpose, I created two more tables in that same database and want to generate domain classes by Reverse Engineering from Hibernate Tools. I followed https://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/ this tutorial. My classes are getting generated but it doesn't contain any annotations or mappings which an entity/domain class should contain. Below is the code generated for reference:

// default package
// Generated Mar 29, 2018 8:18:21 AM by Hibernate Tools 5.2.8.Final

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Product generated by hbm2java
 */
public class Product implements java.io.Serializable {

    private Integer productId;
    private String productName;
    private String createdBy;
    private Date createdDate;
    private String modifiedBy;
    private Date modifiedDate;
    private Set measurmentCategories = new HashSet(0);

    public Product() {
    }

    public Product(String productName) {
        this.productName = productName;
    }

    public Product(String productName, String createdBy, Date createdDate, String modifiedBy, Date modifiedDate,
            Set measurmentCategories) {
        this.productName = productName;
        this.createdBy = createdBy;
        this.createdDate = createdDate;
        this.modifiedBy = modifiedBy;
        this.modifiedDate = modifiedDate;
        this.measurmentCategories = measurmentCategories;
    }

    public Integer getProductId() {
        return this.productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return this.productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCreatedBy() {
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getModifiedBy() {
        return this.modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public Date getModifiedDate() {
        return this.modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public Set getMeasurmentCategories() {
        return this.measurmentCategories;
    }

    public void setMeasurmentCategories(Set measurmentCategories) {
        this.measurmentCategories = measurmentCategories;
    }

}

Though, the class should contain an annotation @Entity, and all variables should contain a proper annotations. But it's not there in the generated code.

Murtaza
  • 308
  • 1
  • 3
  • 18

3 Answers3

3

You need to make sure that you selected the Hibernate version as 5.2.

enter image description here

Hemant Nagpal
  • 624
  • 6
  • 14
1

In “Exporter” tab make sure Generate EJB3 annotations is checked.

Sukhpal Singh
  • 2,130
  • 9
  • 20
  • Sukhpal, by default it is implementing java.io.Serializable interface. Should Spring models/domains implement this interface? Because the Spring tutorial I followed, they didn't implement this interface while creating a domain class manually. – Murtaza Mar 29 '18 at 10:42
  • Not required by the specification though. For more information read similar answer here : https://stackoverflow.com/questions/2020904/when-and-why-jpa-entities-should-implement-serializable-interface – Sukhpal Singh Mar 29 '18 at 14:58
  • I have checked both checkboxes but still, annotations are not generated. I checked each as well (one by one) with no positive results. Each time it generates java classes without annotations. – Hemant Nagpal Apr 15 '20 at 15:10
1

In addition to the Generate EJB3 annotations, make sure to select Annotations (jdk 1.5+) in the hibernate configuration "Main" tab.

Elias N
  • 1,430
  • 11
  • 19
  • 1
    I have checked both checkboxes but still, annotations are not generated. I checked each as well (one by one) with no positive results. Each time it generates java classes without annotations – Hemant Nagpal Apr 15 '20 at 15:26