[jboss-user] [Beginner's Corner] - Re: Persisting java.util.Map with custom class as key and value

piotrekde do-not-reply at jboss.com
Wed Apr 20 15:54:20 EDT 2011


piotrekde [http://community.jboss.org/people/piotrekde] created the discussion

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

To view the discussion, visit: http://community.jboss.org/message/601238#601238

--------------------------------------------------------------
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 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
[http://community.jboss.org/message/601238#601238]

Start a new discussion in Beginner's Corner at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110420/5546600f/attachment.html 


More information about the jboss-user mailing list