在进行一些重构将一些类移动到不同的包中之后,我在使用条件生成器查询数据库时开始看到以下错误:
java.lang.IllegalArgumentException: Parameter value [in.helpi.ironlegion.db.hibernate.entity.UserEntity@1863fc] did not match expected type [in.helpi.ironlegion.cerebro.db.hibernate.entity.UserEntity
如果我将包名称更改回 in.helpi.ironlegion.cerebro.db.hibernate.entity,它就可以正常工作。
更新 我能够正确获取单个实体。但是当我去查询引用其他实体的实体时,我收到此错误。例如:
public class CommunityAccessEntity extends BaseEnity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToOne
@JoinColumn(name = "UserEntity_id")
private UserEntity userEntity;
...
}
如果我使用标准生成器对用户进行查询,例如:
query.select(root).where(criteriaBuilder.equal(root.get(CommunityAccessEntity_.userEntity), user)));
我收到上述错误。
有人也遇到过类似的问题吗..
最佳答案
您必须在实体类中实现 Serialized 接口(interface),因为它是实体类的经验规则之一。
Java 序列化与类名和包名紧密耦合。您的数据与旧包实体一起存储在数据库中。现在您更改了包名称,系统将无法在数据库中找到新包的记录。这就是为什么当您恢复包时,它会起作用。
如果您对 hbm 使用基于 xml 的配置,请检查 hbm 文件是否已在所有位置更新新包。
关于java - 更改 hibernate 托管实体的包名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35739813/