无法确定类型:java.util.List,在表:file_post,对于列:[org.hibernate.mapping.Column(file)]

时间:2019-09-23 19:26:50

标签: java spring hibernate

我试图在数据库中存储文件图像和一些数据,但是在运行应用程序时遇到以下错误。

错误是:

创建名称为'filePostComtroller'的bean时出错:通过字段'filePostService'表示的不满意依赖性;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ filePostServiceImpl”的bean时出错:通过字段“ filePostDAO”表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'filePostDAOImpl'的bean时出错:通过字段'sessionFactory'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-INF/rms-servlet.xml]中定义的名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.MappingException:无法确定类型:java.util.List,在表:file_post,对于列:[org.hibernate.mapping.Column(file)]

FilePost类:

<ul>
  <li v-for="blog in filteredBlogs">
      {{blog.blogName}}
  </li>
</ul>


import blogs from "./../data/blogs";
export default {
    data() {
        return {
            blogs: blogs,
            search:'',
        };
    },
    computed: {
      filteredBlogs:function(){
        return this.blogs.filter((blog) => {
           return blog.blogName.match(this.search);
        });
      }
    }
};

FilePostComtroller:

@Entity
@Table(name="file_post")
public class FilePost implements Serializable {

private static final long serialVersionUID = 74458L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="post_id")
private int postId;

@NotBlank
@Column(name="post_heading")
private String postHeading;

@NotBlank
@Column(name="post_description")
private String postDescription;


@Column(name="post_date")
private String postDate;

@Column(name="file")
private List<MultipartFile> file;


@ManyToOne(fetch=FetchType.LAZY, cascade= {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH })
@JoinColumn(name="user_username")
private User user;

public FilePost() {
    this.user = new User();
}

public FilePost(String postHeading, String postDescription, String postDate, List<MultipartFile> file, User user) {
    this.postHeading = postHeading;
    this.postDescription = postDescription;
    this.postDate = postDate;
    this.file = file;
    this.user = user;
}

public FilePost(int postId, String postHeading, String postDescription, String postDate, List<MultipartFile> file,
        User user) {
    this.postId = postId;
    this.postHeading = postHeading;
    this.postDescription = postDescription;
    this.postDate = postDate;
    this.file = file;
    this.user = user;
}

public int getPostId() {
    return postId;
}

public void setPostId(int postId) {
    this.postId = postId;
}

public String getPostHeading() {
    return postHeading;
}

public void setPostHeading(String postHeading) {
    this.postHeading = postHeading;
}

public String getPostDescription() {
    return postDescription;
}

public void setPostDescription(String postDescription) {
    this.postDescription = postDescription;
}

public String getPostDate() {
    return postDate;
}

public void setPostDate(String postDate) {
    this.postDate = postDate;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public List<MultipartFile> getFile() {
    return file;
}

public void setFile(List<MultipartFile> file) {
    this.file = file;
}

@Override
public String toString() {
    return "FilePost [postId=" + postId + ", postHeading=" + postHeading + ", postDescription=" + postDescription
            + ", postDate=" + postDate + ", file=" + file + "]";
}

FilePostService:

@Controller
public class FilePostComtroller {

@Autowired
private FilePostService filePostService;

@GetMapping("/showFilePostForm")
public String showFilePostForm(Model theModel) {
    FilePost theFilePost = new FilePost();
    theModel.addAttribute("filePost", theFilePost);
    return "filepost-form";
}

@PostMapping("/savePost")
public String uploadFole(@ModelAttribute("filePost") @Valid FilePost theFilePost, BindingResult theResult,
        Principal principal, HttpServletRequest servletRequest) {

    if (theResult.hasErrors()) {
        return "filepost-form";
    }

    //file 
    List<MultipartFile> files = theFilePost.getFile();
    List<String> fileNames = new ArrayList<String>();
    if (null != files && files.size() > 0) {
        for(MultipartFile multipartFile: files) {
            String fileName = multipartFile.getOriginalFilename();
            fileNames.add(fileName);

            File resourcesFile = new File(servletRequest.getServletContext().getRealPath("C:/Users/MD MITHU SARKER/eclipse-workspace/Resource-Management-System/WebContent/resources/file"), fileName);
            try {
                multipartFile.transferTo(resourcesFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // get user name
    String username = principal.getName();
    theFilePost.getUser().setUserName(username);

    //save 
    filePostService.saveFilePost(theFilePost);

    return "filepost-form";
}

}

FilePostServiceImpl:

public interface FilePostService {

void saveFilePost(FilePost theFilePost);

FilePostDAO:

@Service
public class FilePostServiceImpl implements FilePostService {

@Autowired
private FilePostDAO filePostDAO;

@Override
@Transactional
public void saveFilePost(FilePost theFilePost) {
    filePostDAO.saveFilePost(theFilePost);

}

}

FilePostDAOImpl:

public interface FilePostDAO {

void saveFilePost(FilePost theFilePost);

filepost-form.jsp

@Repository
public class FilePostDAOImpl implements FilePostDAO {

// need to inject the session factory
@Autowired
private SessionFactory sessionFactory;

@Override
public void saveFilePost(FilePost theFilePost) {
    Session currentSession = sessionFactory.getCurrentSession();

    currentSession.saveOrUpdate(theFilePost);
}

1 个答案:

答案 0 :(得分:1)

您需要在数据库中将图像另存为blob,并在您的实体中将其声明为字节数组byte[]