/
MySQLBinaryTypeTest.java
82 lines (65 loc) · 1.76 KB
/
MySQLBinaryTypeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.vladmihalcea.hibernate.type.binary;
import com.vladmihalcea.hibernate.util.AbstractMySQLIntegrationTest;
import org.hibernate.annotations.TypeDef;
import org.junit.Test;
import javax.persistence.*;
import static org.junit.Assert.assertArrayEquals;
/**
* @author Vlad Mihalcea
*/
public class MySQLBinaryTypeTest extends AbstractMySQLIntegrationTest {
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Post.class,
};
}
@Test
public void test() {
doInJPA(entityManager -> {
entityManager.persist(
new Post()
.setTitle("High-Performance Java Persistence")
.setImage(new byte[]{1, 2, 3})
);
});
doInJPA(entityManager -> {
Post post = entityManager.find(Post.class, 1L);
assertArrayEquals(
new byte[]{1, 2, 3},
post.getImage()
);
});
}
@Entity(name = "Post")
@Table(name = "post")
public static class Post {
@Id
@GeneratedValue
private Long id;
private String title;
@Column(columnDefinition = "BINARY(3)")
private byte[] image;
public Long getId() {
return id;
}
public Post setId(Long id) {
this.id = id;
return this;
}
public String getTitle() {
return title;
}
public Post setTitle(String title) {
this.title = title;
return this;
}
public byte[] getImage() {
return image;
}
public Post setImage(byte[] image) {
this.image = image;
return this;
}
}
}