-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
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. |
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;
}
} |
@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 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. /**
* 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;
}
} |
@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
} |
Thanks to you, I learned something new! |
I tried adding @column and @Versioin to the getter, but none of them worked.
log output create table sql:
create table contact (id int4 not null, name varchar(255), primary key (id))
The text was updated successfully, but these errors were encountered: