gpt4 book ai didi

java - Hibernate DiscriminatorColumn.Type 不工作

转载 作者:行者123 更新时间:2023-11-29 08:56:07 27 4
gpt4 key购买 nike

我有一个抽象类AbstractTile

@Entity
@Table(name = "TILES")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "t_type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue(value = "abstractTile")
public class AbstractTile {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int id;
//and so on
}

还有很多继承自AbstractTile的类

@Entity
@DiscriminatorValue(value = "dirt")
public class DirtTile extends AbstractTile{
//some code
}

@Entity
@DiscriminatorValue(value = "grass")
public class GrassTile extends AbstractTile{
//some code
}

@Entity
@DiscriminatorValue(value = "water")
public class WaterTile extends AbstractTile{
//some code
}

问题是当我想将它们保存到数据库时,出现错误

Hibernate: 
insert
into
TILES
(/*some data*/, t_type)
values
(/*some data*/, 'dirt')

org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Wrong value for type int : dirt

我做错了什么?DiscriminatorType 设置为 String 那么为什么它需要 int?

编辑:SQL 创建语句

CREATE TABLE tiles
(
t_type character varying(31) NOT NULL,
id serial NOT NULL,
isaccesible boolean,
x integer NOT NULL,
y integer NOT NULL,
map_id integer NOT NULL,
building_id integer,
CONSTRAINT tiles_pkey PRIMARY KEY (id),
CONSTRAINT fk4c20b65b1a758e7 FOREIGN KEY (building_id)
REFERENCES physical_building (building_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk4c20b65bd903ec4 FOREIGN KEY (map_id)
REFERENCES map (map_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE tiles
OWNER TO postgres;

最佳答案

我使用 VARCHAR 类型的 t_type 列运行上面的代码,它运行良好。数据库中的 t_type 列是否为 VARCHAR 类型?

关于java - Hibernate DiscriminatorColumn.Type 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20223694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com