通过Java Test类将原始数据插入数据库

时间:2019-05-15 05:07:28

标签: java hibernate spring-mvc spring-orm

我与已建立的数据库(oracle SQL)建立了连接。我需要在数据库中插入一些凭据(用于登录)。 (在从控制器输入数据之前,我需要这样做)。我真的不明白这里发生了什么或为什么它不起作用。我知道我可以通过数据库手动添加数据,但是我需要能够使用控制器通过Java实现此目的。

//Testing class that is supposed to insert into my database
package com.project;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.project.beans.Creds;
import com.project.beans.Tier;
import com.project.beans.User;
import com.project.dao.CredsDAO;
import com.project.dao.CredsDAOImpl;
import com.project.dao.UserDAO;
import com.project.dao.UserDAOImpl;

public class Driver {

    private static SessionFactory sf;

    public static void main(String[] args) {
            sf = new Configuration().configure().buildSessionFactory();
            sf.openSession();

            UserDAO ud = new UserDAOImpl(sf);
            CredsDAO cd = new CredsDAOImpl(sf);
            Creds creds = new Creds( "cat", "dog");
            User user = new User(1, creds, Tier.BASIC, "Cat", "Dog", "cat@dog.com");
            cd.createCreds(creds);
            ud.createUser(user);

    }

}
//ORMConfiguration
package com.project;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class ORMConfiguration {


    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("com.project.beans");
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl(System.getenv("P2_URL"));
        dataSource.setUsername(System.getenv("P2_USERNAME"));
        dataSource.setPassword(System.getenv("P2_PASSWORD"));

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager hibernateTransactionManager() {
        HibernateTransactionManager transactionManager
          = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    private final Properties hibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty(
          "hibernate.hbm2ddl.auto", "update");
        hibernateProperties.setProperty(
          "hibernate.dialect", "org.hibernate.dialect.Oracle12cDialect");

        return hibernateProperties;
    }

}

//CredsDAOImpl 
package com.project.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.project.beans.Creds;

@Repository(value = "credsDAO")
@Transactional
public class CredsDAOImpl implements CredsDAO {

    private SessionFactory sessionFactory;

    // CONSTRUCTOR INJECTION
    @Autowired
    public CredsDAOImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void createCreds(Creds creds) {
        sessionFactory.getCurrentSession().persist(creds);
    }

    @Override
    public void updateCreds(Creds creds) {
        sessionFactory.getCurrentSession().saveOrUpdate(creds);

    }

    @Override
    public void deleteCreds(Creds creds) {
        sessionFactory.getCurrentSession().delete(creds);
    }
}

//package com.project.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.project.beans.User;


@Repository(value="userDAO")
@Transactional
public class UserDAOImpl implements UserDAO {

    private SessionFactory sessionFactory;

    //CONSTRUCTOR INJECTION
    @Autowired 
    public UserDAOImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    @Override
    public List<User> allUsers() {
        List<User> users = new ArrayList<>();
        Session session = sessionFactory.getCurrentSession();
        users = session.createQuery("from User").getResultList();
        return users;
    }

    @Override
    public User getUserById(int userId) {
        Session session = sessionFactory.getCurrentSession();
        return session.get(User.class, userId);
    }

    @Override
    public void createUser(User user) {
        sessionFactory.getCurrentSession().persist(user);
    }

    @Override
    public void updateUser(User user) {
        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    @Override
    public void deleteUser(User user) {
        sessionFactory.getCurrentSession().delete(user);
    }

}

运行此命令时出现异常:

Exception in thread "main" java.lang.NullPointerException
    at com.project.dao.CredsDAOImpl.createCreds(CredsDAOImpl.java:26)
    at com.project.Driver.main(Driver.java:24)

有什么建议吗? (如果需要,我也可以发布pom)

0 个答案:

没有答案