简体   繁体   English

映射对象流休眠Java EE

[英]Mapping Object Stream hibernate Java EE

I have a mapping problem while trying to map input and output streams into my database . 尝试将输入和输出流映射到数据库时遇到映射问题。 i have tried to make the input streams as Blob but it didn't work . 我试图使输入流成为Blob,但是没有用。 i need to make simple chat , and so the client can send and receive files . 我需要进行简单的聊天,以便客户端可以发送和接收文件。 so that's why i need them to be in the database . 所以这就是为什么我需要它们在数据库中。 I have Also tried to just store the Content of the file but also didn't work . 我也试图只存储文件的内容,但是也没有用。

I have got the exception below : 我有下面的例外:

Caused by: org.hibernate.MappingException: Could not determine type for: java.io.ObjectInputStream, at table: User, for columns: [org.hibernate.mapping.Column(ois)]"}} 原因:org.hibernate.MappingException:无法确定类型:java.io.ObjectInputStream,在表:User处,用于列:[org.hibernate.mapping.Column(ois)]“}}

      @Entity

 public class User implements Serializable {
 private Integer id;
 private Socket sock;
private boolean isConnected;
private ObjectInputStream ois;
private ObjectOutputStream oos;
@Id
@GeneratedValue
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}
public Socket getSock() {
    return sock;
}

public void setSock(Socket sock) {
    this.sock = sock;
}

public boolean isConnected() {
    return isConnected;
}

public void setConnected(boolean isConnected) {
    this.isConnected = isConnected;
}

public  ObjectInputStream getOis() {
    return ois;
}

public void setOis(ObjectInputStream ois) {
    this.ois = ois;
}

public ObjectOutputStream getOos() {
    return oos;
}

public void setOos(ObjectOutputStream oos) {
    this.oos = oos;
}

在此处输入图片说明

If the type of field is Blob, you could use a byte array, something like this: 如果字段类型为Blob,则可以使用字节数组,如下所示:

@Column(name="ois")
private byte[] ois;

public byte[] getOis() {
    return ois;
}

public void setOis(byte[] ois) {
    this.ois = ois;
}

You just need to transform a file to byte array, after that you can store it on your database using Hibernate, for instance: 您只需要将文件转换为字节数组,然后可以使用Hibernate将其存储在数据库中,例如:

File file = new File("C:\\my_file.txt");
byte[] byteFile = new byte[(int) file.length()];

try {
  FileInputStream fileInputStream = new FileInputStream(file);
  fileInputStream.read(byteFile);
  fileInputStream.close();
} catch (Exception e) {
  //Handle the error
}

I hope this information helps you. 希望这些信息对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM