Hugo Tavares
Hugo Tavares

Reputation: 314

hibernate: Missing parameter during merge/insert operation

I want to insert an entry on DB, which has all fields filled and has a relationship of @ManyToOne with another table/class. For that I'm invoking entityManager.merge(c1), where my c1 is a Classification instance (as per the definition below), and I can see on the logs the following:

[DEBUG] ...: c=o.h.SQL, m=logStatement, l=92, msg: insert into plat.classification_system (year, system, id) values (?, ?, ?)
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [1] as [VARCHAR] - [2012]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [2] as [BIGINT] - [1000001]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [3] as [VARCHAR] - [main_system]

...when actually I was expecting to see something like...

[DEBUG] ...: c=o.h.SQL, m=logStatement, l=92, msg: insert into plat.classification_system (year, system, id, classification_type) values (?, ?, ?, ?)
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [1] as [VARCHAR] - [2012]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [2] as [BIGINT] - [1000001]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [3] as [VARCHAR] - [main_system]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [4] as [VARCHAR] - [abc]

I have the following classes

    /* entity being stored */
    @Entity
    @Table("classification_system", indexes = {
        @Index(name="CLASS_IDX1",columnList="ID"),
        @Index(name="CLASS_IDX2",columnList="ID, SYSTEM"),
        @Index(name="CLASS_IDX3",columnList="ID, CLASS_TYPE_GUID, SYSTEM")
    })
    class Classification extends ClassificationAbstract implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="classSeq")
    @SequenceGenerator(name="classSeq", sequenceName="CLASS_SEQ", allocationSize=1)
    private Long id;

    //`enter code here` couple of constructors...

    private String system;

    public Classification() {
        super();
    }

    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getSystem() {
        return this.system;
    }
    public void setSystem(String system) {
        this.system = system;
    }
}

@MappedSuperclass
public abstract class ClassificationAbstract implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(nullable = false)
    @NotNull
    private String year;

    @ManyToOne(fetch = FetchType.EAGER, optional = false, targetEntity = ClassificationType.class)
        @JoinColumns(value = {
            @JoinColumn(name = "YEAR", referencedColumnName = "YEAR", insertable = false, updatable = false),
            @JoinColumn(name = "CLASS_TYPE_GUID", referencedColumnName = "GUID", insertable = false, updatable = false)
    }) //tried using 'true' and doesn't run
    private ClassificationType classificationType;

    public ClassificationType getClassificationType() {
        return classificationType;
    }
    public void setClassificationType(ClassificationType classificationType) {
        this.classificationType = classificationType;
    }

    public String getYear() {
        return this.year;
    }
    public void setYear(String year) {
        this.year = year;
    }
}

/* view CLASS_TYPE */
@Entity
@Table(name="CLASS_TYPE", uniqueConstraints = 
        @UniqueConstraint(columnNames = {"YEAR", "GUID","SYSTEM_TYPE_GUID"})
)
@Immutable
public class ClassificationType extends Type implements Serializable {
    private static final long serialVersionUID = 1L;

    private String code;
    @Column(name = "YEAR", nullable = false)
    private String year;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name = "YEAR", referencedColumnName = "YEAR",
                insertable = false, updatable = false, nullable = false),
        @JoinColumn(name = "SYSTEM_TYPE_GUID", referencedColumnName = "GUID",
                insertable = false, updatable = false, nullable = false)
    })
    private SystemType systemType;

    public String getCode() {
        return this.code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getYear() {
        return this.year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getSystemType() {
        return this.systemType;
    }
    public void setSystemType(String systemType) {
        this.systemType = systemType;
    }
}

/* view SYSTEM_TYPE */
@Entity
@Table(name="SYSTEM_TYPE", uniqueConstraints = 
        @UniqueConstraint(columnNames = {"YEAR", "GUID"})
)
@Immutable
public class SystemType extends Type implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "YEAR", nullable = false)
    private String year;
    private String code;

    public String getCode() {
        return this.code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getYear() {
        return this.year;
    }
    public void setYear(String year) {
        this.year = year;
    }
}

@MappedSuperclass
public abstract class Type implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(length = 48)
    private String guid;

    public String getGuid() {
        return this.guid;
    }
    public void setGuid(String guid) {
        this.guid = guid;
    }
}

...which represents the following model:

    create table classification_system (
     id number(19, 0) not null,
     year varchar2(255 char) not null,
     class_type_guid(48 char ) not null,
     system varchar2(255 char) not null,
     primary key (id)
    )

    create table class_type (
      guid (48 char) not null,
      code varchar2(255 char) not null,
      year varchar2(255 char) not null,
      system_type_guid varchar2(48 char) not null,

      primary key (guid)
    )

    create table system_type (
      guid (48 char ) not null,
      code varchar2(255 char) not null,
      year varchar2(255 char) not null,
      primary key (guid)
    )

I've been digging a lot of posts related to @ManyToOne mappings/relations and did not find anything yet that I can use. This is being built with Hibernate 5.0.9.

Can someone help me out with this? Appreciate any help on the matter.

Upvotes: 0

Views: 542

Answers (1)

Hugo Tavares
Hugo Tavares

Reputation: 314

After all, with skilled help, I ended with the following solution only down to Java instructions and annotations:

  1. Left Classification class as it was

  2. Removed yearattribute from ClassificationAbstract as well the insertable = null, updatable = null annotations:

    @MappedSuperclass
    public abstract class ClassificationAbstract implements Serializable {
        private static final long serialVersionUID = 1L;
    
        @ManyToOne(fetch = FetchType.EAGER, optional = false)
            @JoinColumns(value = {
                @JoinColumn(name = "YEAR", referencedColumnName = "YEAR", nullable = false),
                @JoinColumn(name = "CLASS_TYPE_GUID", referencedColumnName = "GUID", nullable = false)
        })
        private ClassificationType classificationType;
    
        public ClassificationType getClassificationType() {
            return classificationType;
        }
        public void setClassificationType(ClassificationType classificationType) {
            this.classificationType = classificationType;
        }
    }
    
  3. Removed .hashCode() and .equals() definitions from ClassificationType and SystemType, as they were messing up everything at the objects level.

  4. Kept .hashCode() and .equals() definitions in ClassificationTypePK and SystemTypePK. https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/

And now everything is working just fine, and even the JPA/HBMDDL generation is building the expected column definition:

    create table classification_system (
     id number(19, 0) not null,
     year varchar2(255 char) not null,
     class_type_guid(48 char ) not null,
     system varchar2(255 char) not null,
     primary key (id)
    )

    create table class_type (
      guid (48 char) not null,
      code varchar2(255 char) not null,
      year varchar2(255 char) not null,
      system_type_guid varchar2(48 char) not null,

      primary key (guid, year)
    )

    create table system_type (
      guid (48 char ) not null,
      translation varchar2(255 char) not null,
      year varchar2(255 char) not null,
      primary key (guid, year)
    )

Should recall that I'm using Hibernate 5.0.9 (Final), on a Java EE7/Java 8 application, and this was tested against a Oracle XE database with no further issues.

Hope this can be helpful to someone who comes across the same issue.

Upvotes: 1

Related Questions