java - Hibernate sessionfactory 不识别标识符

标签 java mysql eclipse hibernate mapping

我遇到了一个非常奇怪的 hibernate 错误。多亏了 hibernate 代码生成,我已经映射了我的 MySQL 数据库中的所有实体,我已经设置了所有关系并且一切似乎都正常,但现在我遇到了这个错误

org.hibernate.AnnotationException: No identifier specified for entity: com.YouDroop.Data.Entities.Showcase
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:243)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1373)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.tools.hibernate.runtime.common.Util.invokeMethod(Util.java:43)
at org.jboss.tools.hibernate.runtime.common.AbstractConfigurationFacade.buildMappings(AbstractConfigurationFacade.java:160)
at org.hibernate.console.ConsoleConfiguration$4.execute(ConsoleConfiguration.java:272)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:108)
at org.hibernate.console.ConsoleConfiguration.buildMappings(ConsoleConfiguration.java:270)
at org.hibernate.eclipse.console.workbench.ConsoleConfigurationWorkbenchAdapter.getChildren(ConsoleConfigurationWorkbenchAdapter.java:44)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:98)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:232)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

实体Showcase是这个

@Entity
@Table(name = "showcase", schema = "youdroopnorelationships")
public class Showcase implements Serializable {

private static final long serialVersionUID = 1L;

private int code;
private Function function;
private String description;
private int capacity;
private int currentNumberOfProducts;

public Showcase() {
}

public Showcase(int code, Function function, int capacity, int currentNumberOfProducts) {
    this.code = code;
    this.function = function;
    this.capacity = capacity;
    this.currentNumberOfProducts = currentNumberOfProducts;
}

public Showcase(int code, Function function, String description, int capacity, int currentNumberOfProducts) {
    this.code = code;
    this.function = function;
    this.description = description;
    this.capacity = capacity;
    this.currentNumberOfProducts = currentNumberOfProducts;
}

@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "Code", unique = true, nullable = false)
public int getCode() {
    return this.code;
}

public void setCode(int code) {
    this.code = code;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "Function_Name", nullable = false)
public Function getFunction() {
    return this.function;
}

public void setFunction(Function function) {
    this.function = function;
}

@Column(name = "Description", length = 100)
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

@Column(name = "Capacity", nullable = false)
public int getCapacity() {
    return this.capacity;
}

public void setCapacity(int capacity) {
    this.capacity = capacity;
}

@Column(name = "CurrentNumberOfProducts", nullable = false)
public int getCurrentNumberOfProducts() {
    return this.currentNumberOfProducts;
}

public void setCurrentNumberOfProducts(int currentNumberOfProducts) {
    this.currentNumberOfProducts = currentNumberOfProducts;
}

}

如您所见,我已将所有注释写在正确的位置,甚至是 @Id 注释,che .hbm 文件也反射(reflect)了它。 当我重建 Hibernate 配置时出现错误,而如果我只刷新它,它可以工作但不允许我看到 session 工厂,因为这个错误。

最佳答案

确保你有正确的导入:

import javax.persistence.Id;

不是

import org.springframework.data.annotation.Id;

希望这能行得通

关于java - Hibernate sessionfactory 不识别标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33918199/

相关文章:

php - 扩展 MySQLi 类以在页面上解释 SQL 查询

java - Eclipse:资源不在 Java 项目的构建路径上

java - 在 JavaFx 中重新启动应用程序

java - 如何在 Unity 中设置 Java 应用程序名称?

java - 当用户触摸屏幕(外部)时如何隐藏对话框?

java - 放心检查排序

java - 为什么 java.util.function.Consumer 没有 identity()?

java - 提交后 MySQL 与 Hibernate JPA 错误 id

MySQL 查询将 0 添加到数据库但日志却另有说明?

java - 在 Eclipse IDE 中保存操作以将 java 代码转换为菱形表示法