я пытаюсь выучить спящий режим, и это моя первая попытка.Я создал 3 свойства сущностей и хочу создать для них схему, а затем сохранить объекты в БД, используя hibernate.Но когда я пытаюсь выполнить session.save, схема генерируется, а таблицы не заполняются данными.
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String streetName;
public Address() {
}
public Address(String streetName) {
this.setId(-1);
this.streetName = streetName;
}
//some setters/getters
}
ItMan.class
@Entity
public class ItMan implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private int brainpower;
@OneToOne(cascade = CascadeType.ALL)
Address address;
@OneToOne(cascade = CascadeType.ALL)
Phone phone;
public ItMan() {
}
public ItMan(String name, int brainpower, Address address, Phone phone) {
this.setId(-1);
this.name = name;
this.brainpower = brainpower;
this.address = address;
this.phone = phone;
}
//some setters/getters
}
Phone.class
@Entity
public class Phone implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String number;
public Phone() {
}
public Phone(String number) {
this.setId(-1);
this.number = number;
}
//some setters/getters
}
DBService
public class HibernateDBServiceImpl implements DBService {
private final SessionFactory sessionFactory;
public HibernateDBServiceImpl() {
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(Address.class);
configuration.addAnnotatedClass(ItMan.class);
configuration.addAnnotatedClass(Phone.class);
configuration.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
configuration.setProperty("hibernate.connection.url", "jdbc:postgresql:myDB");
configuration.setProperty("hibernate.connection.username", "postgres");
configuration.setProperty("hibernate.show_sql", "true");
configuration.setProperty("hibernate.hbm2ddl.auto", "create");
configuration.setProperty("hibernate.connection.useSSL", "false");
configuration.setProperty("hibernate.enable_lazy_load_no_trans", "true");
sessionFactory = createSessionFactory(configuration);
}
private static SessionFactory createSessionFactory(Configuration configuration) {
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = builder.build();
return configuration.buildSessionFactory(serviceRegistry);
}
@Override
public void save(Object object) {
/*try (Session session = sessionFactory.openSession()) {
session.save(object);
}*/
runInSession(session -> {
session.save(object);
return true;
});
}
@Override
public Object read(Class clazz, String condition) {
long a = Long.getLong(condition);
return runInSession(session -> {
return session.load(clazz, a);
});
}
Как настроить мой код, чтобы он действительно сохранял данные в БД?