Story
My aim is creating a web service for responding as File whose type is oracle.sql.BLOB.
Oh! I could barely find the problem which always shows could not deserialize. At first, I read the section and knew that it was caused by composite key because I did not create both hashCode and equals functions. But it still not worked. Why? See Solution.
I finally inspired by this post.
check whether they are serializable itself and all their fields either serializable too
So that I started searching the method of JPA for Blob and I found this. It described how to insert and retrieve Blob type. Anyway, let’s see the sample.
Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.entity; | |
import java.math.BigDecimal; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.IdClass; | |
import javax.persistence.Lob; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name = "Sample") | |
@IdClass(SampleCPK.class) | |
public class Sample { | |
@Id | |
@Column(nullable=false) | |
private String id; | |
@Id | |
@Column(nullable=false) | |
private BigDecimal sNo; | |
@Column | |
private String attachName; | |
@Column | |
private String attachType; | |
@Lob | |
@Column | |
private byte[] attachFile; | |
/* | |
* This constructor is only for JPA. | |
* */ | |
Sample() { | |
} | |
/* | |
Getters & Setters... | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.entity; | |
import java.io.Serializable; | |
import java.math.BigDecimal; | |
public class SampleCPK implements Serializable { | |
private String id; | |
private BigDecimal sNo; | |
public SampleCPK(){} | |
public String getId() { | |
return id; | |
} | |
public BigDecimal getSNo() { | |
return sNo; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((id == null) ? 0 : id.hashCode()); | |
result = prime * result + sNo.intValue(); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
SampleCPK other = (SampleCPK) obj; | |
if (id == null) { | |
if (other.id != null) | |
return false; | |
if (sNo != other.sNo) | |
return false; | |
return true; | |
} | |
} |