使用hibernate我遇到了架构验证问题。错误非常简单,它表示我的代码中预期的列类型和实际找到的列类型不匹配。
除此之外我认为他们确实匹配。在查看类似问题之后我添加了columnDefinition =“int(10)”,但这没有帮助。任何有关进一步研究的帮助或指示都将非常感激。
类定义和变量声明
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类
@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
表格轨迹
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;
错误讯息:
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
答案 0 :(得分:0)
不明白为什么你需要为int类型显式columnDefinition="int(10)"
。我的建议是删除它,让Hibernate通过设置hibernate.hbm2ddl.auto=create
或update
来生成表格。
另请参阅:https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html
答案 1 :(得分:0)
Hibernate不是在抱怨Traject而是关于tt_traject_ref_toets
wrong column type encountered in column [traject_id] in table [tt_traject_ref_toets]
这是一个映射错误:
ToetsBijTraject
正在映射一个关联表。
您可以使用@ManyToMany映射(仅在您只有id列的情况下)
或者您必须将映射更改为此
@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;
MapsId告诉hibernate那些是pk / fk,它和声明一样 @JoinColumn(“trajectId”,insertable = false,updatable = false)
你可以在这里找到一些好的解释
此外,您不需要columnDefinition