3

I created database, which have a references with ENUM table and I have exception in entity of this enum.

scripts example:

CREATE TABLE status (
  code VARCHAR(40),
  status ENUM('not started', 'in progress', 'finished')
);

insert into status (code, status)
values (1, 'not started'),
       (2, 'in progress'),
       (3, 'finished');

CREATE TABLE `explorer` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `create_date` datetime DEFAULT NULL,
  `query` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `status_id` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (status_id) REFERENCES status(code)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

So, after creating table I did create automatically entities for this tables:

  • Explorer:

    @Entity
    @Table(name = "explorer", schema = "parsebeandeveloper", catalog = "")
    public class ExplorerEntity {
    private long id;
    private Timestamp createDate;
    private String query;
    private String title;
    private Integer statusId;
    
    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    
    @Basic
    @Column(name = "create_date")
    public Timestamp getCreateDate() {
        return createDate;
    }
    
    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }
    
    @Basic
    @Column(name = "query")
    public String getQuery() {
        return query;
    }
    
    public void setQuery(String query) {
        this.query = query;
    }
    
    @Basic
    @Column(name = "title")
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    @Basic
    @Column(name = "status_id")
    public Integer getStatusId() {
        return statusId;
    }
    
    public void setStatusId(Integer statusId) {
        this.statusId = statusId;
    }
    }
    

class, where I get exception:

  • Status:

    @Entity
    @Table(name = "status", schema = "parsebeandeveloper")
    public class StatusEntity {
    private Integer code;
    private Object status;
    
    @Basic
    @Column(name = "code")
    public Integer getCode() {
        return code;
    }
    
    public void setCode(Integer code) {
        this.code = code;
    }
    
    @Basic
    @Column(name = "status")
    public Object getStatus() {
        return status;
    }
    
    public void setStatus(Object status) {
        this.status = status;
    }
    

    }

That, what I get in console:

org.springframework.beans.factory.BeanCreationException:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa /HibernateJpaConfiguration.class]: Invocation of init method failed; nested > exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: property mapping has wrong number of columns: com.sb.bean.parser.explorer.model.domain2.StatusEntity.status type: > object

I have think, that I did make mistake

  • in the creating table explorer, in string:

      FOREIGN KEY (status_id) REFERENCES status(code)
    
  • or in creating class EntityStatus

How to correctly create Status entity or create references between tables in this case?

Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59

1 Answers1

10

Hibernate complains that it is not able to map StatusEntity.status because it is declared Object type.

You can change it to String type which Hibernate should be able to map to database ENUM type.

You can also use Java enum for status field and have hibernate map it using @Enumerated annotation.

tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • So basically we can't have an object that includes a sub-object / object from another table? – Collin Sep 29 '22 at 15:33