Announcement Announcement Module
Collapse
No announcement yet.
ClassCastException in find Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException in find

    I execute the find of JpaTemplate class and the result is a ClassCastException, how to fix this?

    In the code when the line "Subscription subsc = (Subscription) subscriptions.get(0)" is executed the exception appears.

    My service class:
    Code:
    public class ServicesManagerImpl extends JpaDaoSupport implements ServicesManager {
    
    private void findBySubscription(String idSub) {
    
            List<Subscription> subscriptions =
                    getJpaTemplate().find("SELECT s FROM Subscription s WHERE s.subscriptionPK.id= ?1", idSub);
    
            Subscription subsc = (Subscription) subscriptions.get(0);
    }
    My entity class

    Code:
    @Entity
    @Table(name = "SUBSCRIPTION")
    public class Subscription implements Serializable {
    
        private static final long serialVersionUID = -2290713792736331110L;
        
        @EmbeddedId
        protected SubscriptionPK subscriptionPK;
    
        @Column(name = "DATE_INI")
        private Date  dateInit;
    
    ...
    }
    Code:
    @Embeddable
    public class SubscriptionPK implements Serializable {
    
        private static final long serialVersionUID = 7526487931062728752L;
    
        @Basic(optional = false)
        @Column(name = "id")
        private String id;
        
        @Basic(optional = false)
        @Column(name = "APP_ID")
        private long appId;
    ...
    }

  • #2
    Remove "SELECT s" from your query.

    Comment


    • #3
      Doesn´t work!

      Comment


      • #4
        Ah, you are using JPA, so you are right, select is not optional like HQL.
        Check to see what your find method returns. I guess it is a list of Object[]. In that case your get(0) returns an array which you try to cast into Subscription.

        Comment

        Working...
        X