如何使用hibernate在数据库中插入视频文件?

时间:2017-04-25 21:41:52

标签: java mysql hibernate video pojo

世界各地的程序员我在使用hibernate在我的mysql数据库中插入视频文件时遇到问题,我希望在这里找到解决方案。

我有这个pojo来映射数据库表。

@Entity

public class Client实现Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String dni;
private String nom;
private Date neix;
@Column(columnDefinition = "LONGBLOB") 
private byte[] image;

public Client() {
}

public Client(String dni, String nom, Date neix, byte[] image) {
    this.dni = dni;
    this.nom = nom;
    this.neix = neix;
    this.image = image;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getDni() {
    return dni;
}

public void setDni(String dni) {
    this.dni = dni;
}

public String getNom() {
    return nom;
}

public void setNom(String nom) {
    this.nom = nom;
}

public Date getNeix() {
    return neix;
}

public void setNeix(Date neix) {
    this.neix = neix;
}

public byte[] getImage() {
    return image;
}

public void setImage(byte[] image) {
    this.image = image;
}

@Override
public String toString() {
    return "Client{" + "id=" + id + ", dni=" + dni + ", nom=" + nom + ", neix=" + neix + ", image=" + image + '}';
}

这是我主要将客户端保存到我的数据库中,我尝试使用图像,它可以工作,但没有视频文件。

public static void main(String[] args) throws ParseException, IOException {
    Service s = new Service();

    Client c1 = new Client();
    c1.setDni("123412");
    c1.setNeix(new Date("2003/11/24"));
    c1.setNom("SNK");
    c1.setImage(readBytesFromFile("C:\\Users\\Adrikus\\Documents\\Mipony\\snk2x1.mp4"));
    s.addClient(c1);
}

private static byte[] readBytesFromFile(String filePath) throws FileNotFoundException, IOException {
    File inputFile = new File(filePath);
    FileInputStream inputStream = new FileInputStream(inputFile);

    byte[] fileBytes = new byte[(int) inputFile.length()];
    inputStream.read(fileBytes);
    inputStream.close();

    return fileBytes;
}

这是由hibernate抛出的错误信息:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.io.ByteArrayOutputStream.<init>(ByteArrayOutputStream.java:77)
at com.mysql.jdbc.PreparedStatement.setBytes(PreparedStatement.java:3407)
at com.mysql.jdbc.PreparedStatement.setBytes(PreparedStatement.java:3382)
at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$1.doBind(VarbinaryTypeDescriptor.java:62)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:282)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2818)
at org.hibernate.persister.entity.AbstractEntityPersister$4.bindValues(AbstractEntityPersister.java:3025)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3556)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:97)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:480)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:191)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:175)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:210)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
at Dao.AccountDao.saveClient(AccountDao.java:49)

提前感谢您试图帮助我:)

0 个答案:

没有答案