-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with Enum as String mapping - bug?
PostPosted: Wed Oct 31, 2012 7:33 am 
Newbie

Joined: Tue Oct 30, 2012 6:34 pm
Posts: 1
Hello,

I have a problem with such a mapping

Code:
@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @Column(name = "id", unique = true)
    private String id;

    @MapKeyColumn(name = "social_network")
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private Map<SocialNetwork, SocialNetworkProfile> socialNetworkProfiles
            = new EnumMap<SocialNetwork, SocialNetworkProfile>(SocialNetwork.class);

    protected User() {}

    public User(SocialNetwork sn, String socialNetworkId) {
        SocialNetworkProfile profile = new SocialNetworkProfile(this, sn, socialNetworkId);
        socialNetworkProfiles.put(sn, profile);
    }

    public SocialNetworkProfile getSocialNetworkProfile(SocialNetwork socialNetwork) {return socialNetworkProfiles.get(socialNetwork);}
}

@Entity
@Table(name = "social_network_profile", uniqueConstraints = {@UniqueConstraint(columnNames = {"social_network", "network_id"})})
public class SocialNetworkProfile {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @Column(name = "id", unique = true)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @Enumerated(value = EnumType.STRING) //if change type to ordinal - test will not failure
    @Column(name = "social_network", nullable = false)
    private SocialNetwork socialNetworkType;

    @Column(name = "network_id", nullable = false)
    private String networkId;

    protected SocialNetworkProfile() {}

    protected SocialNetworkProfile(User user, SocialNetwork socialNetworkType, String networkId) {
        this.user = user;
        this.socialNetworkType = socialNetworkType;
        this.networkId = networkId;
    }
}

public enum SocialNetwork {
    STUB_NETWORK_NAME
}


When I try to load User#socialNetworkProfiles by find user and call User#getSocialNetworkProfile I receive such exception

On PostgreSQL:
Code:
Caused by: org.postgresql.util.PSQLException: Bad value for type int : STUB_NETWORK_NAME
   at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toInt(AbstractJdbc2ResultSet.java:2759)
   at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2003)
   at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2426)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:104)


On H2:
Code:
Caused by: java.lang.NumberFormatException: For input string: "STUB_NETWORK_NAME"
   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
   at java.lang.Integer.parseInt(Integer.java:449)
   at java.lang.Integer.parseInt(Integer.java:499)
   at org.h2.value.Value.convertTo(Value.java:809)


So, it tries to cast "STUB_NETWORK_NAME" to int, like it is ORDINAL mapping here:
@Enumerated(value = EnumType.STRING) //if change type to ordinal - test will not failure
@Column(name = "social_network", nullable = false)
private SocialNetwork socialNetworkType;

When I change EnumType to ORDINAL here - it works. It looks like a bug, but maybe I'm wrong and have some problems in my mapping or config? As you see, I tried PosgreSQL and H2 and have the same ussue on both.

You can find simple example on github public repo: github.com/Spikhalsky/broken-hibernate-enum-mapping
There is one unit test with name SimpleTest there - it is failure with String mapping and works fine with Ordinal, you can run maven build to check it.
I use the latest 4.1.7.Final hibernate-core version.

Thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.