Is there any way I can use JPA or hibernate annotations to specify the exact type and length (according to MySQL) the field should be in the database?
For example:
@Entity
@Table { appliesTo="test" }
public class Test implements Serializable {
private string info;
public void setInfo(String i) {
info = i;
}
@Column { name="id", length="25" }
public String getInfo() {
return info;
}
}
I would like to reverse-engineer this class using the hibernate-maven-plugin and I would like to add “varchar 35” or something to the @Column annotation so that when I do the reverse engineering the fields are created as I specify and with lengths I specify.
Also, what is the difference between using the “Column” annotation on a java field (eg private long age) vs a getter method (Where I normally see it)?
Yes there is. Attribute columnDefinition in Column annotation is there for such a needs. If such a detailed control over generated SQL DDL is not needed – length, precision, and scale attributes can be useful.
You should not spread persistence annotations to both fields and methods. As long as they are consistently placed, it will be fine. Persistence provided (like Hibernate) chooses does it use fields or methods to persist values based to where annotations are located. If they in both, result is undefined and unlikely to work.