Answer the question
In order to leave comments, you need to log in
Java how to finally run Hibernate?
Hello everyone, I have already read a bunch of Hibernate 5 tutorials and some kind of boilerplate everywhere, I refuse to believe that in 2018 you need to create a bunch of .xml files to describe each Entity and the configuration of Hibernate itself. (Maybe there are some libraries for generating xml?)
I found this option (from the official docs):
Configuration cfg = new Configuration()
.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/indicators_value")
.setProperty("hibernate.connection.username", "postgres")
...
.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect")
.setProperty("hibernate.connection.datasource", "java:com/driver/db/entity")
.setProperty("hibernate.order_updates", "true")
.addClass(MyClass.class);
org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : com/driver/db/entity/MyClass.hbm.xml
Session session = cfg.configure().buildSessionFactory().openSession();
session.beginTransaction();
String sql = "select version()";
String result = (String) session.createNativeQuery(sql).getSingleResult();
System.out.println(result);
session.getTransaction().commit();
session.close();
.addClass(MyClass.class);
Answer the question
In order to leave comments, you need to log in
Hello.
First you need to have an annotated class associated with an entity in the database. Here is an example from my project:
@Entity
@Table(name = "users")
public class User implements Serializable {
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "users_user_id_seq")
@SequenceGenerator(name = "users_user_id_seq",
sequenceName = "users_user_id_seq",
allocationSize = 1)
private long userId;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "login", nullable = false, unique = true)
private String login;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "enabled", nullable = false)
private boolean isEnabled;
@Column(name = "reg_date", nullable = false)
private Date regDate;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "authority_id", nullable = false)
private Authority authority;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@JsonIgnore
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
@JsonSerialize(using = SqlDateSerializer.class)
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
@Override
public String toString() {
return "User details" +
"Id = " + getUserId() + "\n" +
"Name = " + getName() + "\n" +
"Login = " + getLogin() + "\n" +
"Email = " + getEmail() + "\n" +
"Enabled = " + isEnabled() + "\n" +
"Role = " + getAuthority().getAuthority().name() + "\n" +
"Registration date = " + getRegDate();
}
.addClass(MyClass.class)
, but configuration.addAnnotatedClass(Main.class)
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question