hibernate模式验证错误的列但使用正常的int

2024-03-03

使用 hibernate 时我遇到了架构验证问题。该错误非常简单,它表示我的代码中预期的列类型和实际找到的列类型不匹配。

但据我所知,它们确实匹配。在查看类似问题后,我添加了columnDefinition =“int(10)”,但这没有帮助。任何帮助或指示进一步查看什么将不胜感激。

类定义和变量声明

轨迹类

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

Hibernate 抱怨的不是 Traject,而是tt_traject_ref_toets

wrong column type encountered in column [traject_id] in table [tt_traject_ref_toets]

这是一个映射错误:

the 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”,可插入= false,可更新= false)

你可以在这里找到一些很好的解释

https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/ https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/

另外你也不需要columnDefinition

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hibernate模式验证错误的列但使用正常的int 的相关文章

随机推荐