无法通过休眠事务来实际填充表。表格是空的

时间:2018-12-19 04:03:19

标签: java database postgresql hibernate

我试图学习休眠,这是我的第一次尝试。我制作了3个实体属性,并希望为其创建架构,然后使用休眠将对象存储到DB中。但是,当我尝试执行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);
    });
}

我如何调整代码,使其实际将数据保存到数据库?

2 个答案:

答案 0 :(得分:0)

解决了删除配置属性的问题:

configuration.setProperty("hibernate.hbm2ddl.auto", "create");

不知道到底发生了什么,也许有人在评论中可以解释这一点:为什么我删除它-我使休眠状态将数据保存在DB中,并且如果我保持此配置并且甚至不再次启动程序(这将清除)该配置属性的所有方案cos)我在DB中没有数据。

答案 1 :(得分:0)

configuration.setProperty("hibernate.hbm2ddl.auto", "create");

这将在您每次运行代码时从头开始创建架构。您应该将“创建”更改为“更新”或“无”

这是文档:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html