gpt4 book ai didi

带枚举的 Hibernate 查找表

转载 作者:行者123 更新时间:2023-12-04 17:52:01 26 4
gpt4 key购买 nike

我目前正在尝试学习 hibernate,遇到了一个从 OOP 和 DB 的角度来看都合理的问题,但我无法找到解决方案。

我的 java 部分看起来大致像这样:

@Entity
@Table (name = "Vehicles")
class Vehicle {
@Id
@Column (name = "UUID")
UUID id;
VehicleType type

(getters, setters, other values, etc.)
}

enum VehicleType {
TRUCK,
TRAILER,
OTHER;
}

我的数据库部分有两个表 - 一个用于车辆,一个用于车辆类型的查找表:

车辆

UUID VARCHAR (PK)
vehicle_type_id INT (FK to VehicleTypes)

车辆类型

vehicle_type_id INT (PK)
vehicle_type VARCHAR

我的问题是,是否可以按照呈现的方式进行安排?如果可以,如何实现(注释等)。如果不是,实现类似结构的最佳方式是什么?

-----编辑-----

我已经(或多或少)使用 solution 实现了我的目标由 Anthony Patricio 描述。

DB 部分保持不变,而 Java 部分如下所示:

@Entity
@Table (name = "Vehicles")
class Vehicle {
@Id
@Column (name = "UUID")
UUID id;
@Column (name = "vehicle_type_id")
@Type (type = "VehicleEnumUserType")
VehicleType type

(getters, setters, other values, etc.)
}

enum VehicleType {
TRUCK(1),
TRAILER(2),
OTHER(0);

private int value;
VehicleType(int value){
this.value = value;
}

public int getValue() {
return value;
}

public static VehicleType fromValue(int value){
for (VehicleType type : VehicleType.values()){
if (value == type.value) return type;
}
throw new IllegalArgumentException("Value not mapped by VehicleType enum");
}
}

VehicleEnumUserType implements UserType{
private static final int[] SQL_TYPES = {Types.INTEGER};

public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o)
throws HibernateException, SQLException {
int number = resultSet.getInt(strings[0]);
return resultSet.wasNull() ? null : VehicleType.fromValue(number);
}

public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor)
throws HibernateException, SQLException {
if (o == null){
preparedStatement.setNull(i, Types.INTEGER);
} else {
preparedStatement.setInt(i, ((VehicleType)o).getValue());
}
}
// other mandatory methods required by UserType interface
}

这个解决方案符合我的要求——车辆类型在数据库端(通过查找表限制)和 Java 端(通过 VehicleType 枚举)都受到限制。此外,车辆类型存储为整数,减少了存储数据所需的空间。最后但并非最不重要的是,此解决方案允许非常安全地添加新车辆类型(例如,与 EnumType.ORDINAL 相关的问题)。

希望任何人都觉得这个解决方案有用。

最佳答案

你可以使用“枚举”注解

@Enumerated(EnumType.STRING)
VehicleType type

关于带枚举的 Hibernate 查找表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912784/

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