JBoss Community

Re: Persisting java.util.Map with custom class as key and value

created by piotrekde in Beginner's Corner - View the full discussion

Thanks for help!

I think that the first one option is the easiest one (blob). Can I store object as a blob in a database, but have it defined as a Map type in class properties? What I mean is this (MyEntity class, myBlobMap property):

 

 

 @Local
    public static interface MyEntityRepository {
        void persist(MyEntity entity);
        MyEntity find(long id);
    }
 
 
    @Stateful
    public static class MyEntityRepositoryBean implements MyEntityRepository {
 
 
    @PersistenceContext(unitName = "mydb")
    private EntityManager entityManager;
 
 
        public void persist(MyEntity entity) {
            entityManager.persist(entity);
        }
 
 
        public MyEntity find(long id) {
            return entityManager.find(MyEntity.class, id);
        }
    }
 
 
    @Entity
    @Table(name = "my_entity")
    private class MyEntity {
 
 
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private long id;
 
 
        @Lob
        @Column(name="my_blob")
        private Map<CustomClassA, CustomClassB> myBlobMap;
 
 
        public MyEntity() {
            this.myBlobMap = new LinkedHashMap<CustomClassA, CustomClassB>();
        }
 
 
        public long getId() {
            return this.id;
        }
 
 
        public void putToMap(CustomClassA a, CustomClassB b) {
            this.myBlobMap.put(a,b);
        }
 
 
        public Map<CustomClassA, CustomClassB> getMyBlobMap() {
            return this.myBlobMap;
        }
    }
 
 
    private class CustomClassA implements Serializable {
 
 
        private double a;
        private double b;
 
 
        public CustomClassA(double a, double b) {
            this.a = a;
            this.b = b;
        }
 
 
        public double getA() {
            return this.a;
        }
 
 
        public double getB() {
            return this.b;
        }
    }
 
 
    private class CustomClassB implements Serializable {
        private String x;
 
 
        public CustomClassB(String x) {
            this.x = x;
        }
 
 
        public String getX() {
            return this.x;
        }
    }
 

 

 

anyway, if I try to test this code, I'm getting an exception:

 

 

 @Test
    public void testBlobPersistence() throws Exception {
        MyEntityRepository repo  = (MyEntityRepository) initialContext.lookup(
                "MyEntityRepositoryBeanLocal");
 
        MyEntity entity = new MyEntity();
        //entity.putToMap(new CustomClassA(1,2), new CustomClassB("qwertyz"));
        repo.persist(entity);
        MyEntity persisted = repo.find(entity.getId());
 
        //assertNotNull(persisted.getMyBlobMap());
    }
 

 

 

(...)

Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.sql.Blob

(...)

 

Do I have to store it as a blob internally? I found the similar issue here:

http://docs.jboss.org/ejb3/app-server/tutorial/blob/src/org/jboss/tutorial/blob/bean/LobTesterBean.java

( #findBlob:HashMap)

Reply to this message by going to Community

Start a new discussion in Beginner's Corner at Community