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);
@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)
, а configuration.addAnnotatedClass(Main.class)
.