I have now managed to get the SQLServer code to work with Lobs. However, I am unable to get it to work properly with Postgres, now.
Here is what I have now:
Code:
@Entity
@Table(name = "my_image")
public class MyImage implements java.io.Serializable {
private String uniqueId;
private Date lastModified;
private byte[] image;
private String imageType;
private Message message;
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "unique_id", unique = true, nullable = false, length = 32)
public String getUniqueId() {
return this.uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_modified", nullable = false, length = 35)
public Date getLastModified() {
return this.lastModified;
}
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
@Lob
@Column(name = "image")
public byte[] getImage() {
return this.image;
}
public void setImage(byte[] image) {
this.image = image;
}
@Column(name = "image_type", length = 3)
public String getImageType() {
return imageType;
}
public void setImageType(String type) {
this.imageType = type;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "message_unique_id", nullable = false)
public Message getMessage() {
return this.message;
}
public void setMessage(Message message) {
this.message = message;
}
}
@Entity
@Table(name = "message")
public class Message {
private String uniqueId;
private Date timeStamp;
private Double cost;
private Double weigth;
private List<MyImage> myImages= new ArrayList<MyImage>(
0);
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "unique_id", unique = true, nullable = false, length = 32)
public String getUniqueId() {
return this.uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "time_stamp", length = 35)
public Date getTimeStamp() {
return this.timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
@Column(name = "cost", precision = 17, scale = 17)
public Double getCost() {
return this.cost;
}
public void setCost(Double cost) {
this.cost = cost;
}
@Column(name = "weigth", precision = 17, scale = 17)
public Double getWeigth() {
return this.weigth;
}
public void setWeigth(Double weigth) {
this.weigth = weigth;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "message")
public List<MyImage> getMyImages() {
return this.myImages;
}
public void setMyImages(
List<MyImage> myImages) {
this.myImages = myImages;
}
public void addImage(MyImage img) {
this.getMyImages().add(img);
img.setLastModified(new Date(System.currentTimeMillis()));
img.setMessage(this);
}
}
It seems like I can insert data correctly. However, when I try to fetch the data, I get this error:
Code:
org.hibernate.exception.GenericJDBCException: could not initialize a collection: [db.model.Message.myImages#402881e51a35fa21011a35fc18790069]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1992)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at handler.command.MessageCommandTest.testHandleFileCONWithTestImage(MessageCommandTest.java:349)
The line that causes the error looks like this:
Code:
fetch = callDao.findById(fetch.getUniqueId());
assertEquals(2, fetch.getMyImages().size());
The second line causes problems. Prior to that line, I create and store images using the DAO I've created.
The findById() method does this:
Code:
public Message findById(String id) {
log.debug("getting Message instance with id: " + id);
try {
Message instance = getEntityManager().find(Message.class, id);
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
Any idea what prevents it from working on Postgres?
Thanks,
L