[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):

    public static interface MyEntityRepository {
        void persist(MyEntity entity);
        MyEntity find(long id);
    public static class MyEntityRepositoryBean implements MyEntityRepository {
    @PersistenceContext(unitName = "mydb")
    private EntityManager entityManager;
        public void persist(MyEntity entity) {
        public MyEntity find(long id) {
            return entityManager.find(MyEntity.class, id);
    @Table(name = "my_entity")
    private class MyEntity {
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private long id;
        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) {
        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:

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

> (...)
> 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

Start a new discussion in Beginner's Corner at Community

-------------- 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