2016-12-30 162 views
1

我想在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 项目成功运行。但数据没有插入。 是否可以在没有休眠的情况下使用其他方法插入数据。如果有人对此有所了解,我将不胜感激。

+0

考虑发布做全堆栈跟踪 – 2016-12-30 10:48:10

回答

3

你不指定javax.persistence封装为Id注释。

通过

import javax.persistence.Id; 
+0

更换

import org.springframework.data.annotation.Id; 

。有用。 –

+0

@Downvoter它是免费的吗? – davidxxx

+0

@Anupam Agnihotri伟大:) – davidxxx

1

您使用的是错误@Id注释(import org.springframework.data.annotation.Id;

使用import javax.persistence.Id;代替