zwwcn

Just another WordPress.com site

Monthly Archives: June 2014

find object can’t be serialized

I encountered a problem today :one of the object can't be serialized, then I found
this useful code on from http://www.theserverside.com/discussions/thread.tss?thread_id=17336
       public static final Object clone(Serializable in) {
            try {
                ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
                ObjectOutputStream outStream = new ObjectOutputStream(byteOutStream);
                outStream.writeObject(in);
                ByteArrayInputStream byteInStream =
                    new ByteArrayInputStream(byteOutStream.toByteArray());
                ObjectInputStream inStream = new ObjectInputStream(byteInStream);
                return inStream.readObject();
            } catch (OptionalDataException e) {
             throw new RuntimeException("Optional data found. " + e.getMessage()); 
            } catch (StreamCorruptedException e) {
             throw new RuntimeException("Serialized object got corrupted. " + e.getMessage());
            } catch (ClassNotFoundException e) {
             throw new RuntimeException("A class could not be found during deserialization. " + e.getMessage()); 
            } catch (NotSerializableException ex) {
                ex.printStackTrace();
             throw new IllegalArgumentException("Object is not serializable: " + ex.getMessage());
            } catch (IOException e) {
             throw new RuntimeException("IO operation failed during serialization. " + e.getMessage()); 
            }
        }
Advertisements

Hibernate one to many filtering

Recently I need to do an hiberate one to many filtering, but it’s not as easy as I thought.

Firstly, hibernate criteria doesn’t allow you join the same association twice, which mean you can’t have code like this:


finalCriteria.createAlias("a.b","b1", JoinType.LEFT_OUTER_JOIN)

.createAlias("a.b","b2", JoinType.LEFT_OUTER_JOIN);

Two workarounds are mentioned on https://hibernate.atlassian.net/browse/HHH-879, I tried use detachedCriteria but got the same error.

The second solution works for me:


Criterion  criterion  = Restrictions.sqlRestriction(" ((exists (select tc.OBJECT_ID from OP_TICKET_CATEGORY_OPERATOR tc where tc.TICKET_CATEGORY_ID = this_.OBJECT_ID AND tc.OPERATOR_ID ="+selectedResolver.getObjID()+" AND tc.OPERATOR_TYPE='RE')) AND (exists (select tc.OBJECT_ID from OP_TICKET_CATEGORY_OPERATOR tc where tc.TICKET_CATEGORY_ID = this_.OBJECT_ID AND tc.OPERATOR_ID ="+selectedOpener.getObjID()+" AND tc.OPERATOR_TYPE='OP')))" );
finalCriteria.add(criterion);

 

Entities:


OP_TICKET_CATEGORY

@OneToMany(mappedBy="ticketCategory",fetch=FetchType.LAZY,orphanRemoval=true)
private List<TicketCategoryOperator> ticketCategoryOperators;

------------------------------------------------

OP_TICKET_CATEGORY_OPERATOR

@ManyToOne
@JoinColumn(name = "TICKET_CATEGORY_ID",nullable = true)
private TicketCategory ticketCategory;

@ManyToOne
@JoinColumn(name = "OPERATOR_ID",nullable = true)
private Operator operator;

TicketCategoryOperatorType type = TicketCategoryOperatorType.OPENER;