0

How to configure database configuration and mapping in Java file without using hibernate.cfg.xml file?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.