Hey there i m trying to save an object which name is "Avatar" and then retrieve the object and display it's picture "image". i got this result :
my Avatar Class:
@Entity
@Table(name="avatar")
public class Avatar implements java.io.Serializable {
@Id
private Integer avatarId;
@Column(name = "image")
private byte[] image;
//getters and setters
How i m saving the object:
session.getTransaction().begin();
File file = new File("PicturePath");
byte[] bFile = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Avatar avatar = new Avatar();
avatar.setAvatarId(1);
avatar.setImage(bFile);
session.save(avatar);
session.getTransaction().commit();
Retrieving the data from Avatar Table:
public List<Avatar> avatarList(){
session.getTransaction().begin();
List<Avatar> avatar = session.createQuery("from Avatar").list();
session.getTransaction().commit();
return avatar;
}
Controller:
@FXML
public TableView<Avatar> produits;
@FXML
public void initialize(){
Task<ObservableList<Avatar>> task = new Task<ObservableList<Avatar>>() {
@Override
protected ObservableList<Avatar> call() throws Exception {
return FXCollections.observableArrayList(CategorieTrait.getInstance().avatarList());
}
};
produits.itemsProperty().bind(task.valueProperty());
new Thread(task).start();
}
My FXML file:
<TableView style="-fx-backround-color:#33d9b2;" fx:id="produits" prefWidth="900" prefHeight="600">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
<columns>
<TableColumn text="Picture" style="-fx-backround-color:#33d9b2">
<cellValueFactory>
<PropertyValueFactory property="image"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
I was expecting the actual picture to display in the tableview.