Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing @Column on field and @Transient on getter and setter at once

Please, explain for what purpose @Transient was placed on getter and setter methods at the same time as @Column was placed on field. The field is stored to DB as we need.

@Entity

@Table(name = "person")

public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "name")
    private String name; 

    @Transient
    public long getName() {
        return name;
    }

    @Transient
    public void setName(String name) {
        this.name = name;
    }
}
like image 637
user6705928 Avatar asked Dec 10 '25 13:12

user6705928


1 Answers

In the code as you have it, they have no effect, since the @Id on a field causes the default access to be field, thus ignoring any method annotations. If property access was the default (either by @Id on the getter or @Access(PROPERTY) on the class), the @Transient annotations would cause JPA to ignore the accessors, presumably so that the field mapping could be picked up. However, in that case, the field should be annotated with @Access(FIELD).

I'd say the @Transient annotations are leftovers from a time when the entity had default property access.

like image 56
ehecatl Avatar answered Dec 13 '25 02:12

ehecatl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!