java - Spring , hibernate : issue with creating new @Entity model

标签 java mysql hibernate spring-mvc persistence

我正在尝试在 mysql 表 testdata 中插入数据。

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;


import org.springframework.data.annotation.Id;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;


import org.springframework.data.annotation.Id;

@Entity
@Table(name = "testdata")
public class testdata {


    @Id
    @Column(name = "image_id")
    private String image_id;


    @Column(name = "post_id")
    String post_id;

    @Column(name = "image")
     String image;

    public String getPost_id() {
        return post_id;
    }

    public void setPost_id(String post_id) {
        this.post_id = post_id;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getImage_id() {
        return image_id;
    }

    public void setImage_id(String image_id) {
        this.image_id = image_id;
    }

    public testdata(String post_id, String image, String image_id) {
        super();
        this.post_id = post_id;
        this.image = image;
        this.image_id = image_id;
    }

}

public String getPost_id() {
    return post_id;
}

public void setPost_id(String post_id) {
    this.post_id = post_id;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getImage_id() {
    return image_id;
}

public void setImage_id(String image_id) {
    this.image_id = image_id;
}

public testdata(String post_id, String image, String image_id) {
    super();
    this.post_id = post_id;
    this.image = image;
    this.image_id = image_id;
}

当我运行我的项目时出现以下异常

springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.demo.service.ServiceInterface com.demo.controller.UserController.service; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.demo.dao.UserDao com.demo.service.ServiceImpl.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.demo.dao.UserDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.demo.conf.HibernateConfiguration: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.demo.model.testdata

但是如果我删除@Entity 项目运行成功。但数据没有插入。 是否可以在没有 hibernate 的情况下使用其他方法插入数据。 如果有人对此有所了解,我将不胜感激。

最佳答案

您没有为 Id 注释指定 javax.persistence 包。

替换

import org.springframework.data.annotation.Id;

import javax.persistence.Id;

关于java - Spring , hibernate : issue with creating new @Entity model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41394865/

相关文章:

c# MySQL like 查询不带参数

java - Spring/Hibernate - 在 Java 中不使用 @Entity 从存储过程中获取结果

java - 从 Java 调用 .NET DLL

java - Jshell 错误 : java. lang.NullPointerException: charsetName

java - Spring 3.0 中 Post 函数不绑定(bind)

java - hibernate 迁移:BeanlibException

java - 在 hibernate 中包含 log4j 属性文件以显示带有值而不是问号的查询

java - 检查动态创建的类是否是一个 Activity

mysql - 安装 MySql 并在 Docker 镜像中加载 Dump

mysql - 查询花费很长时间在错误日志中发现锁定代码?