Skip to content

JPA annotations is doesn't work on getter #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Michael1024-CJX opened this issue May 16, 2021 · 8 comments
Closed

JPA annotations is doesn't work on getter #2216

Michael1024-CJX opened this issue May 16, 2021 · 8 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@Michael1024-CJX
Copy link

I tried adding @column and @Versioin to the getter, but none of them worked.

@Entity
@Table(name = "contact")
public class ContactEO {
    @Id
    private Integer id;
    private String name;

    @Column(name = "contact_name", nullable = false, length = 512)
    public String getName() {
        return name;
    }
    // other getter setter
}

log output create table sql:
create table contact (id int4 not null, name varchar(255), primary key (id))

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 16, 2021
@tco0427
Copy link

tco0427 commented May 16, 2021

I'm not sure, but it seems that Getter is not working properly because the maximum length (255) of the table and the character length of the annotation do not match the constraints.

@Michael1024-CJX
Copy link
Author

@tco0427 I tried to change the value of length to 10, but SQL still uses the name attribute instead of the contact_name I hope. It seems that the program did not parse the getter method of @column, I am not sure whether it is because of Hibernate or Spring.

@tco0427
Copy link

tco0427 commented May 16, 2021

I think you want to do as follows.

@Entity
@Table(name="contact")
public class ContactE0 {
    @Id
    private Integer id;

    private String contactName;
    @Column(name="name", nullable = false,length=512)
    public String getName(){
        return contactName;
    }
}

@Michael1024-CJX
Copy link
Author

@tco0427 https://github.com/Michael1024-CJX/Test-JPA/tree/master ,I wrote a simple spring-data-jpa demo, when the application is started, the database table will be created automatically, but the field is not the value of the name in @column that I want. If you have time, can you help me see what's wrong?

@tco0427
Copy link

tco0427 commented May 16, 2021

In such cases, it is reasonable to give Column annotation to the field.
And if you do that, you'll be able to create the table you want.
like this
스크린샷 2021-05-16 오후 7 33 56

@Michael1024-CJX
Copy link
Author

@tco0427 Thank you for your help, the definition of @column on the Field can indeed take effect, but I want to study how to define @column on the getter.
Because I want to implement this kind of interface:

/**
 * If the entity needs to implement optimistic locking, implement this interface
 */
public interface Verifiable<T> {
    @Version
    T getVersion();
}

@Entity
@Table(name="someEntity")
public class someEntity implements Verifiable<Integer> {
    @Id
    private Integer id;

    private Integer version;

    @Override
    public Integer getVersion() {
        return version;
    }
}

@Michael1024-CJX
Copy link
Author

@tco0427 I know the reason!!! Hibernate does not support mixing field annotations and getter method annotations, you can only choose one of them. So I only need to define @id on the getter.

@Entity
@Table(name = "contact")
public class ContactEO {
    private Integer id;
    private String name;
    @Id  //It's important here
    private Integer getId() {
        return id;
    }
    @Column(name = "contact_name", nullable = false, length = 50)
    public String getName() {
        return name;
    }
    // setter
}

@tco0427
Copy link

tco0427 commented May 16, 2021

Thanks to you, I learned something new!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants