-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why do I need a setter ?
PostPosted: Fri Jan 29, 2010 12:30 pm 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Hi,

Let's say I have an entity with automatic id generation. In this case, I don't want to have a setter, just a getter. This works fine when I put the @Id annotation on an attribute :

Code:
@Entity
public class Country implements Serializable {

    @Id @GeneratedValue
    private Long id;
    private String code;
    private String name;

    public Long getId() {
        return id;
    }
    ...
}


Now, when I move the @Id annotation to the getter, I get an exception saying that I need a setter :

Code:
@Entity
public class LongTermAssetRating implements Serializable {

    private Long id;
    private String code;
    private String name;

    @Id @GeneratedValue
    public Long getId() {
        return id;
    }

    // Needs a setter to work
    public void setId(Long id) {
        this.id = id;
    }
    ...
}


Code:
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property id in class fr.bdf.staticData.domain.StaticDataSecurity
   at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:240)
   at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:233)
   at org.hibernate.mapping.Property.getSetter(Property.java:299)


Why is that ? I thought annotations could work seemlessly on attributes or getters. In this case, it forces me to have a setter but I don't want it. When you look at the Hibernate code (org.hibernate.property.BasicPropertyAccessor.createSetter(Class theClass, String propertyName)) it clearly throws an exception if the setter doesn't exist.

Any idea on that ?
Thanks,
Antonio


Top
 Profile  
 
 Post subject: Re: Why do I need a setter ?
PostPosted: Fri Jan 29, 2010 12:49 pm 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
What I can do though, is have a private setId() instead of public.


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

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.