当前位置: 首页 > 知识库问答 >
问题:

无法确定类型:java.util.列表,表:file_post,列:[org.hibernate.mapping.列(文件)]

东方俊明
2023-03-14

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

错误是:

创建名为“filePostComtroller”的bean时出错:通过字段“filePostService”表示未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“filePostServiceImpl”的bean时出错:通过字段“filePostDAO”表示未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“filePostDAOImpl”的bean时出错:通过字段“sessionFactory”表示未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建名为“sessionFactory”的bean时出错,该bean在ServletContext资源[/WEB-INF/rms servlet.xml]中定义:调用init方法失败;嵌套的异常是org。冬眠MappingException:无法确定java的类型。util。列表,在表中:file_post,列:[org.hibernate.mapping.Column(文件)]

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

FilePostComtroller:

@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>

共有1个答案

隆康平
2023-03-14

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

 类似资料: