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

标签 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 类:

@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 + "]";
}

文件后 Controller :

@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";
}

文件后服务:

public interface FilePostService {

void saveFilePost(FilePost theFilePost);

}

FilePostServiceImpl:

@Service
public class FilePostServiceImpl implements FilePostService {

@Autowired
private FilePostDAO filePostDAO;

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

}

FilePostDAO:

public interface FilePostDAO {

void saveFilePost(FilePost theFilePost);

}

FilePostDAOImpl:

@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);
}

filepost-form.jsp

<form:form action="savePost" modelAttribute="filePost" method="POST" enctype="multipart/form-data">

<label>Post Heading:</label><br>
<form:input type="text" path="postHeading" name="postHeading"/><br><br>
<form:errors path="postHeading"></form:errors>

<label>Post Description:</label><br>
<form:input type="text" path="postDescription" name="postDescription"/><br><br>
<form:errors path="postDescription"></form:errors>

<label>Post Date:</label><br>
<form:input type="date" path="postDate" name="postDate"/><br><br>
<form:errors path="postDate"></form:errors>

<label for="file">Post File: </label><br>
<form:input type="file" path="file" name="file" multiple="multiple"/><br><br>

<input type="submit" value="Submit"/>

</form:form>

最佳答案

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

关于java - 无法确定 : java. util.List 的类型,位于表 : file_post, 的列 : [org. hibernate.mapping.Column(file)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58068966/

相关文章:

java - EJB SessionBean - @TransactionManagement 和@TransactionAttribute 的组合

java - 数独 block 检查器 Java

java - 具有自定义集合名称的 Spring Data MongoDB 存储库

java - 每个 spring 源代码 ide 的 springframework.org/tags 表单的 Maven 引用?

hibernate - 如何使用 JPA CriteriaBuilder 查询执行外连接?

java - hibernate : convert complicated hql to criteria

java - 在centos for java上安装opencv

java - 使用最新的 BaseGameUtils 的 Google Play 游戏服务 LibGDX 项目设置

spring - Grails + hibernate + Controller sessionFactory空对象?

hibernate - java.sql.SQLException : Invalid value for getInt() - 'foo'