Ich entwickle eine Dokument-Management-Anwendung in spring
mit jpa
und MySQL
. Die Anwendung akzeptiert derzeit ein Dokument und seine Metadaten von einem Benutzer-Webformular createOrUpdateDocumentForm.jsp
in den Controller DocumentController.java
. Die Daten gelangen jedoch nicht in die MySQL
-Datenbank. Kann mir jemand zeigen, wie ich meinen Code ändern kann, damit das Dokument und seine Metadaten in der zugrunde liegenden Datenbank gespeichert werden?Dokument nicht speichern im Frühjahr jpa Dokument-Manager-Anwendung
Der Fluss der Daten (einschließlich der PDF-Dokument) über die folgenden Objekte zu gehen scheint:
createOrUpdateDocumentForm.jsp //omitted for brevity, since it is sending data to controller (see below)
Document.java
DocumentController.java
ClinicService.java
JpaDocumentRepository.java
The MySQL database
I relevanten Teile jedes dieser Objekte zusammenfassen wie folgt:
Die jsp
der Auslöser folgende Verfahren in DocumentController.java
:
@RequestMapping(value = "/patients/{patientId}/documents/new", headers = "content-type=multipart/*", method = RequestMethod.POST)
public String processCreationForm(@ModelAttribute("document") Document document, BindingResult result, SessionStatus status, @RequestParam("file") final MultipartFile file) {
document.setCreated();
byte[] contents;
Blob blob = null;
try {
contents = file.getBytes();
blob = new SerialBlob(contents);
} catch (IOException e) {e.printStackTrace();}
catch (SerialException e) {e.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}
document.setContent(blob);
document.setContentType(file.getContentType());
document.setFileName(file.getOriginalFilename());
System.out.println("----------- document.getContentType() is: "+document.getContentType());
System.out.println("----------- document.getCreated() is: "+document.getCreated());
System.out.println("----------- document.getDescription() is: "+document.getDescription());
System.out.println("----------- document.getFileName() is: "+document.getFileName());
System.out.println("----------- document.getId() is: "+document.getId());
System.out.println("----------- document.getName() is: "+document.getName());
System.out.println("----------- document.getPatient() is: "+document.getPatient());
System.out.println("----------- document.getType() is: "+document.getType());
try {System.out.println("[[[[BLOB LENGTH IS: "+document.getContent().length()+"]]]]");}
catch (SQLException e) {e.printStackTrace();}
new DocumentValidator().validate(document, result);
if (result.hasErrors()) {
System.out.println("result.getFieldErrors() is: "+result.getFieldErrors());
return "documents/createOrUpdateDocumentForm";
}
else {
this.clinicService.saveDocument(document);
status.setComplete();
return "redirect:/patients?patientID={patientId}";
}
}
Wenn ich einreichen ein Dokument über das Web-Formular im jsp
mit dem controller
die System.out.println()
Befehle im controller
Codeausgabe der folgenden, die anzeigen, dass die Daten in der Tat an den Server gesendet zu werden:
----------- document.getContentType() is: application/pdf
----------- document.getCreated() is: 2013-12-16
----------- document.getDescription() is: paper
----------- document.getFileName() is: apaper.pdf
----------- document.getId() is: null
----------- document.getName() is: apaper
----------- document.getPatient() is: [[email protected] id = 1, new = false, lastName = 'Frank', firstName = 'George', middleinitial = 'B', sex = 'Male', dateofbirth = 2000-11-28T16:00:00.000-08:00, race = 'caucasian']
----------- document.getType() is: ScannedPatientForms
[[[[BLOB LENGTH IS: 712238]]]] //This indicates the file content was converted to blob
Das Document.java
Modell ist:
@Entity
@Table(name = "documents")
public class Document {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@ManyToOne
@JoinColumn(name = "client_id")
private Patient patient;
@ManyToOne
@JoinColumn(name = "type_id")
private DocumentType type;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@Column(name="filename")
private String filename;
@Column(name="content")
@Lob
private Blob content;
@Column(name="content_type")
private String contentType;
@Column(name = "created")
private Date created;
public Integer getId(){return id;}
public void setId(Integer i){id=i;}
protected void setPatient(Patient patient) {this.patient = patient;}
public Patient getPatient(){return this.patient;}
public void setType(DocumentType type) {this.type = type;}
public DocumentType getType() {return this.type;}
public String getName(){return name;}
public void setName(String nm){name=nm;}
public String getDescription(){return description;}
public void setDescription(String desc){description=desc;}
public String getFileName(){return filename;}
public void setFileName(String fn){filename=fn;}
public Blob getContent(){return content;}
public void setContent(Blob ct){content=ct;}
public String getContentType(){return contentType;}
public void setContentType(String ctype){contentType=ctype;}
public void setCreated(){created=new java.sql.Date(System.currentTimeMillis());}
public Date getCreated() {return this.created;}
@Override
public String toString() {return this.getName();}
public boolean isNew() {return (this.id == null);}
}
The ClinicService.java
-Code, der von der DocumentController
ist aufgerufen:
private DocumentRepository documentRepository;
private PatientRepository patientRepository;
@Autowired
public ClinicServiceImpl(DocumentRepository documentRepository, PatientRepository patientRepository) {
this.documentRepository = documentRepository;
this.patientRepository = patientRepository;
}
@Override
@Transactional
public void saveDocument(Document doc) throws DataAccessException {documentRepository.save(doc);}
Th e entsprechenden Code in JpaDocumentRepository.java
ist:
@PersistenceContext
private EntityManager em;
@Override
public void save(Document document) {
if (document.getId() == null) {this.em.persist(document);}
else {this.em.merge(document);}
}
die relevanten Teile des SQL-Code schließlich, die die Datenbank enthalten erstellt:
CREATE TABLE IF NOT EXISTS documenttypes (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
INDEX(name)
);
CREATE TABLE IF NOT EXISTS patients (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30),
middle_initial VARCHAR(5),
last_name VARCHAR(30),
sex VARCHAR(20),
date_of_birth DATE,
race VARCHAR(30),
INDEX(last_name)
);
CREATE TABLE IF NOT EXISTS documents (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
client_id int(4) UNSIGNED NOT NULL,
type_id INT(4) UNSIGNED,
name varchar(200) NOT NULL,
description text NOT NULL,
filename varchar(200) NOT NULL,
content mediumblob NOT NULL,
content_type varchar(255) NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (client_id) REFERENCES patients(id),
FOREIGN KEY (type_id) REFERENCES documenttypes(id)
);
Welche Änderungen kann ich an diesem Code machen, so dass es speichert die document
in der documents
Tabelle der MySQL
Datenbank mit jpa
?
Ich gehe davon aus, dass es keine Fehler, und dass andere Daten sind auf die DB begangen? –
@ user2310289 Ich habe während dieser Operation keine Fehler in der Eclipse-Konsole gesehen. Ein anderes Modul in dieser Anwendung schreibt Daten zum Hinzufügen einer Person zur DB fest, aber das Modul zum Hinzufügen von Dokumenten schreibt keine Daten fest. Der einzige Fehler tritt beim Laden der App in Tomcat Server, aber die App lädt und dann der Code in der aktuellen Frage ausgeführt wird, ohne einen Fehler zu werfen. Wenn Sie denken, dass der Fehler beim Laden der Anwendung relevant ist, können Sie ihn in diesem Beitrag betrachten: http://stackoverflow.com/questions/20622746/exception-loading-sessions-from-persistent-storage – CodeMed
"The Beim Laden der App in den Tomcat Server kommt nur ein Fehler hinzu "Was ist das? Auch, sind Ihre Form auf Jsp mit diesem Attribut: 'enctype =" multipart/form-data "' –