问题描述
我正在尝试在包级别创建一个 @GenericGenerator 注释,以便包中的所有实体都可以使用它.
I'm trying to create a @GenericGenerator annotation at the package level so it can be used by all the entities in the package.
1) 我有一个带有注释的 package-info.java 类:
1) I have a package-info.java class with the annotation:
@org.hibernate.annotations.GenericGenerator(name="unique_id", strategy="uuid")
package com.sample.model;
2) 在同一个包中,我有一个具有以下属性的实体:
2) In that same package, I have an entity with the following attribute:
@Id
@GeneratedValue(generator="unique_id")
@Column(name="userid")
public String userID() {
return userID;
}
这会导致异常Unknown Id.generator: unique_id".如果我在实体类中包含 @GenericGenerator 注释,它工作正常.但是,我想将其移动到包级别,以便我可以在其他实体中重用它.
This is resulting in an exception "Unknown Id.generator: unique_id". If I include the @GenericGenerator annotation in the entity class, it works fine. However, I want to move this to the package level so I can reuse it in other entities.
断开连接的任何想法?
谢谢!
推荐答案
我知道这篇文章可能很旧......但我搜索了那个问题的解决方案,我找到了一个对我有用的解决方案.所以我会把它贴在这里,以防它对某人有帮助:
I know that this post may be old ... but I searched for the solution of that problem and I found a solution that worked for me. So I'll post it here in case it would be helpful for someone :
我忘记在 cfg.xml 中添加包级别信息
I forgot to add the package level information in the cfg.xml
也许您应该在 cfg.xml 中添加这一行:
Maybe you should add this line in your cfg.xml :
<mapping package="com.sample.model"/>
它对我有用:)
这篇关于在包级别注释中使用 @GenericGenerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!