views:

53

answers:

3

Hi I am new to Java persistence. Does it matter where I put the Annotations at? Are the following two sets of the code the same?

Thanks a lot!!

@Entity
@Table(name="user", schema="billing")
public class User {
    private long id ;
    private String userName ;
    private String password ;

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Column(name="password", length=20)
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
    @Column(name="user_name",length=15)
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}

}

Compare to:

@Entity
@Table(name="user", schema="billing")
public class User {
    @Id
    @GeneratedValue
    private long id ;
    @Column(name="user_name",length=15)
    private String userName ;
    @Column(name="password", length=20)
    private String password ;    

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
    public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}

}

+1  A: 

Pretty much, yes. The JPA implementation will look at your annotations to see which style you prefer, and work with that.

Hibernate, I know, looks to see where the @Id annotation is (on the field or on the method), and uses that style for the rest of the annotations. I imagine other JPA implementations do something similar, if not the same.

skaffman
A: 

Check this similar question out:-

Hibernate/JPA - annotating bean methods vs fields

Strawberry
A: 

I would though highly recommend taking a look at JPersist (http://www.jpersist.com/) where you don't need any annotations or XML configuring. Much easier then the weirdness you have there

TheLQ