Hibernate/JPA - Annotating bean methods vs fields


Benstpierre :

I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean.

My question is the following: with JPA annotations (eg @Id), is there a difference between annotating fields and bean methods?

example:

@Entity
public class User
{

**@ID**
private int id;

public int getId(){
return this.id;
}

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

}

- - - - - -or- - - - - -

@Entity
public class User
{


private int id;

**@ID**
public int getId(){
return this.id;
}

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

}
Duffy:

Yes, I believe you want to search for site visits vs property visits:

Hibernate annotation - which is better, field access or property access?

Spring preference is field access . This is what I follow.

Related


Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

How to inject fields in bean methods?

kaylil_01: Sonar-linter thinks the correct way to inject is: @Bean public Example example(DataSource datasource) { return new Example(datasource) } But if only one method is using this field. I'm curious, why is there only one way? Maybe @Autowired is bett

How to inject fields in bean methods?

kaylil_01: Sonar-linter thinks the correct way to inject is: @Bean public Example example(DataSource datasource) { return new Example(datasource) } But if only one method is using this field. I'm curious, why is there only one way? Maybe @Autowired is bett

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Can Spring @Bean methods return instance fields?

mkjeldsen : The following Spring bean declaration appears to work and behave "as expected": @Configuration public class AppConfig { private final Foo foo; public AppConfig() { foo = new Foo(); } @Bean public Foo foo() { r

Preserve constructor when annotating fields

Nason: I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the c

Preserve constructor when annotating fields

Nason I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the co

Preserve constructor when annotating fields

Nason I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the co

Preserve constructor when annotating fields

Nason: I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the c

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotation to

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotation to

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but you don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotatio

Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I'm creating a controller where for each method I have to annotate it like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset

Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I am creating a controller where I have to annotate each method like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset=utf-8

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but you don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotatio

Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I'm creating a controller where for each method I have to annotate it like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset

Property methods vs fields in C#

Dashnick Using C# - if I have a property of an object that, for example, returns the number of elements in that object's fields array, like NumColsthis: public string[][] Table { get; } // Jagged 2D Table public int NumRows { get { return Table.Length; } } /