Working with hibernate I'm stuck on a schema validation issue. The error is pretty straightforward, it says the expected column type and actual found ones in my code don't match.
Except that as far as I can see they do match. I've added columnDefinition="int(10)" after looking at similar questions, but this doesn't help. Any help or pointers what to look at further would be much appreciated.
Class definition and variable declaration
Traject class
public class Traject implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", columnDefinition="int(10)")
private int id;
@OneToMany (fetch = FetchType.EAGER, mappedBy = "traject")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "toets_bij_traject")
@JsonBackReference
@OrderBy("toetsInSerie ASC")
private Set<ToetsBijTraject> toetsenBijTraject;
other vars and methods not shown
ToetsBijTraject class
@Entity
@Table(name = "tt_traject_ref_toets")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="")
public class ToetsBijTraject implements Serializable, Comparable<ToetsBijTraject> {
@Id
@Column(name = "traject_id")
private Traject traject;
@Id
@Column(name = "toets_id")
private Toets toets;
other vars and methods not shown
Table traject
CREATE TABLE `traject` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`naam` varchar(20) NOT NULL,
`is_actief` tinyint(1) DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `naam` (`naam`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
CREATE TABLE `tt_traject_ref_toets` (
`traject_id` int(10) unsigned NOT NULL,
`toets_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`traject_id`,`toets_id`),
KEY `toets_id` (`toets_id`),
CONSTRAINT `tt_traject_ref_toets_ibfk_1` FOREIGN KEY (`traject_id`)REFERENCES `traject` (`id`),
CONSTRAINT `tt_traject_ref_toets_ibfk_2` FOREIGN KEY (`toets_id`)
REFERENCES `ref_toets` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Error message:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [traject_id] in table [tt_traject_ref_toets]; found [int (Types#INTEGER)], but expecting [tinyblob (Types#VARBINARY)]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateColumnType(SchemaValidatorImpl.java:105)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:92)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:484)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
... 87 more
Hibernate is complaining not about Traject but about tt_traject_ref_toets
wrong column type encountered in column [traject_id] in table [tt_traject_ref_toets]
It's a mapping mistake:
the ToetsBijTraject is mapping an associative table.
You can either use the @ManyToMany mapping (only in case where you have only the id columns)
or you have to change the mapping to this
@Entity
@Table(name = "tt_traject_ref_toets")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="")
public class ToetsBijTraject implements Serializable, Comparable<ToetsBijTraject> {
@Id
@Column(name = "traject_id")
private int trajectId;
@Id
@Column(name = "toets_id")
private int toetsId;
@ManyToOne
@MapsId("trajectId")
private Traject traject;
@ManyToOne
@MapsId("toetsId")
private Toets toets;
The MapsId tells hibernate that those are pk/fk and its the same as declaring @JoinColumn("trajectId", insertable=false, updatable=false)
You can find some good explanation here
https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/
Also you have no need of columnDefinition
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With