gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:30:00 25 4
gpt4 key购买 nike

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

错误是:

创建名称为“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com