How to configure database configuration and mapping in Java file without using hibernate.cfg.xml file?
1 Answer
hibernate.cfg.xml is old school - I haven't used it for years. Use the javax.persistance annotations and annotate your classes - hibernate does the rest.
Here's a very basic example:
import javax.persistence.*;
@Entity
@Table(name = "my_table")
public class MyTable {
private Integer id;
private String someColumn;
@Id
@GeneratedValue
@Column(name = "my_table_id", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "some_column")
public String getSomeValue() {
return someColumn;
}
public void setSomeColumn(String someColumn) {
this.someColumn = someColumn;
}
}
See this documentation for an explanation of all hibernate annotations.