-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Binary types for Postgresql and SQL Server (and possibly Fir
PostPosted: Wed Apr 30, 2008 3:55 pm 
Newbie

Joined: Thu Sep 20, 2007 1:33 pm
Posts: 8
Hibernate version:

3.2.1 ga
entity-manager 3.3.1 ga
annotations 3.0.0 ga

Full stack trace of any exception that occurs:

Code:
javax.persistence.PersistenceException: org.hibernate.HibernateException: Wrong column type: image, expected: varbinary(255)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:737)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)

Caused by: org.hibernate.HibernateException: Wrong column type: image, expected: varbinary(255)
   at org.hibernate.mapping.Table.validateColumns(Table.java:261)
   at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1080)
   at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:730)
   ... 26 more



Name and version of the database you are using:

SQL Server 2000
jtds-1.2.2
Postgresql 8.2/8.3

The generated SQL (show_sql=true):


Sample SQL Server code (entered manually):

Code:
CREATE TABLE my_image
(
   [unique_id]                      [varchar](32) NOT NULL,
   [image]                          [image],
   [last_modified]                  [datetime] NOT NULL,
   [image_type]                     [varchar](3)
) ;


Sample Postgres code:

Code:
CREATE TABLE my_image
(
   unique_id                varchar(32) NOT NULL,
   image                    bytea,
   last_modified            timestamp with time zone NOT NULL,
   image_type               character varying(3)
) ;


Java code looks like this:

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;

   public DisposalMessageImage() {
   }

   @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;
   }

   @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;
   }

}


It works fine for Postgres but fails for SQL server with the above message.

How does this need to be set so the entity works for both? The image is of arbitrary length.

Thanks,

L


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 01, 2008 3:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
I would suggest you to use
Code:
@Lob


but anyway it would be interesting to understand how it happened that the SQL server schema defined to column type to "image";
Is that Hibernate's automatically generated schema? did you use any XML mapping in addition to this code?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 01, 2008 2:11 pm 
Newbie

Joined: Thu Sep 20, 2007 1:33 pm
Posts: 8
The SQL code is written by hand, it is not generated by the persistence engine. That's what I meant by "entered manually".

Hmmm... adding @Lob then requires the Postgres type to be oid instead of bytea. I've never worked with oid so I'm not sure.

When I run it with SQL Server, I still get an error saying that the expected type is varbinary(255) and that image is not a correct type.

Is it Hibernate that determines the expected type or is it the jTDS driver?

L


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 2:59 pm 
Newbie

Joined: Thu Sep 20, 2007 1:33 pm
Posts: 8
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 3:27 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
did you enable SQL logging?
Code:
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 11:05 am 
Newbie

Joined: Thu Sep 20, 2007 1:33 pm
Posts: 8
Actually, yes. I get this:

Code:
10:59:40,179 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: 25P01
10:59:40,179 ERROR [JDBCExceptionReporter] Large Objects may not be used in auto-commit mode.


When I set the autocommit flag to false I see this:

Code:
11:01:50,241 WARN  [Ejb3Configuration] hibernate.connection.autocommit = false break the EJB3 specification


And actually, that goes though my test cases, it seems.

Should I be worried about the warning?

L


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.