简体   繁体   中英

Java annotation fields setter and getter how to

What I want to accomplish but don't understand really how it works its

@Table(name = "Categories")
public class Category extends Model { 
    @Column(name = "Name")
    private String name;
}

With this code I want to annotation generate their respective setter and getter so I can use like

Category category = new Category();
category.setName("My Name");
category.save();

Whey I need setter and getter and not access/edit the value directly? Because some values has different treatment, like relations, and because I want to have fields that don't want to be edited. And I'm too lazy to do manually all the time this, with every model, also save a lot of work later to just put an annotation and set their field

My inspiration to try this was Android Annotations seems a solid and cool library, I know maybe its too advanced but my goal with this experiment its to have a library like that but focused on models like active record or another orm.

Tuts, tips, advices, books are welcome.

Regards

Edit 2013-10-25

My goal is to build a library capable to do this, because I'm too curious and want to learn how internally work, so I'll be able to power my framework with this feature, as jet just are small utilities but in the future I hope it save me a lot of work, you can see at github WSD Android

If you are too lazy to create the setters and getters of your variables why not just let your IDE generate it for you?

but if you do really insist, take a look at this plugin

This simply allows you to do this

@Table(name = "Categories")
public class Category extends Model { 

    @Setter
    @Getter
    @Column(name = "Name")
    private String name;
}

WARNING.

the plugin is not well documented, you also need to configure your IDE to actually see it(eg category.getName() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM