Hibernate > CLOB > Oracle :(

后端 未结 4 2171
北荒
北荒 2020-12-06 14:52

I am trying to write to an Oracle clob field a value over 4000 characters. This seams to be a common issue but non of the solutions seem to work. So I pray for help from her

4条回答
  •  被撕碎了的回忆
    2020-12-06 15:29

    Thanks to non sequitor for all the help. I have this working and figure I will put all the pieces here for future reference. Regardless of all the claims about upgrading the drivers and everything would work, non of that worked for me. In the end I had to implement a 'org.hibernate.usertype.UserType' I named it the same as all the examples on the web StringClobType. Save for some imports I used the example from Using Clobs/Blobs with Oracle and Hibernate. As far as I am concerned ignore the "beware" claim.

    There was one change I had to make to get merges to work. Some of the methods were not implemented in the provided code sample. Eclipse fixed it for me by stubbing them out. Cool, but the replace method needs to be actually implemented or all merges will overwrite the data with a null. Here is my implementation:

    public Object replace(Object newValue, Object existingValue, Object arg2)throws HibernateException {
        return newValue;
    }
    

    I will not duplicate the class implementation here go to the above link to see it. I used the code in the third gray box. Then at the top of the pojo class I wanted to use it in I added the following after the imports

    ...  
    import org.hibernate.annotations.Type;  
    import org.hibernate.annotations.TypeDefs;  
    import org.hibernate.annotations.TypeDef;  
    
    @TypeDefs({  
        @TypeDef(  
            name="clob",  
            typeClass = foo.StringClobType.class  
        )  
    })  
    @Entity  
    @Table(name="EA_COMMENTS")  
    public class Comment extends SWDataObject implements JSONString, Serializable {  
    ...  
    }   
    

    Then to use the new UserType I added the annotation to my getter:

    @Type(type="clob")
    @Column(name="COMMENT_DOC")
    public String getDocument(){
        return get("Document");
    }
    

    I did not need the @Lob annotation.
    In my persistence.xml the persistence-unit declaration ended looking like:

    
        org.hibernate.ejb.HibernatePersistence
        
             
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    
    

    The SetBigStringTryClob never worked for me and was not needed for this final implementation.

    My lesson learned is in the end it is probably better to join then to fight. It would of saved me three days.

提交回复
热议问题