Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package myDarkDiary.service.model; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; import org.apache.commons.io.IOUtils; @Entity @Table(name = "images") public class Image { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER) @JoinColumn(nullable = false, name = "userId") private User userId; private String fileName; private byte[] data; public Image(User userId, String fileName, String filePath) throws IOException { this.userId = userId; this.fileName = fileName; ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(filePath).getFile()); InputStream targetStream = new FileInputStream(file); this.data = IOUtils.toByteArray(targetStream); } public Image() { } public long getId() { return id; } public void setId(long id) { this.id = id; } public User getUserid() { return userId; } public void setUserid(User userId) { this.userId = userId; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public byte[] getData() { return data; } public void setData(byte[] data) { this.data = data; } }