How to map byte[] to jdbc BINARY type?

I used hibernate 5.2.12 with mysql database.

I have following class (simplified):

@Embeddable
@Access( AccessType.FIELD )
public class IpAddress implements Serializable,Comparable<IpAddress>{

    @Column(name = "ip_address", length = 17)
    @Type(type = "org.hibernate.type.BinaryType")
    private byte[] ipBytes;
}

I want that ipBytes to be mapped to BINARY(17) mysql type,and i try to use @Type annotation, but there is no effect, hibernate map ipBytes to TINYBLOB.

I tried to use columnDefinition = “BINARY(17)”, and it work’s, but i don’t know is it safe to use, and also it database dependent.

Also tried UUIDBinaryType and hibenrnate map ipBytes right(to BINARY(17)), but when i try to insert there is an exception, because uuid must be 16 bytes length value.

How can i force hibernate to save ipBytes as BINARY type?

It’s actually very simple.

Since version 2.10.1, you can use the MySQLBinaryType from the Hibernate Types project, so your mapping looks like this:

@Entity
@TypeDef(
	typeClass = MySQLBinaryType.class, 
	defaultForType = byte[].class
)
public class Post {

	@Id
	@GeneratedValue
	private Long id;

	private String title;

	@Column
	private byte[] image;
	
	...
}

Here you can see an example on GitHub that works like a charm.