2

I need to write some temporary code in my existing Spring Boot 1.2.5 application that will do some complex SQL queries. By complex, I mean a single queries about 4 different tables and I have a number of these. We all decided to use existing SQL to reduce potential risk of getting the new queries wrong, which in this case is a good way to go.

My application uses JPA / Hibernate and maps some entities to tables. From my research it seems like I would have to do a lot of entity mapping.

I tried writing a class that would just get the Hibernate session object and execute a native query but when it tried to configure the session factory it threw an exception complaining it could not find the config file.

Could I perhaps do this from one of my existing entities, or at least find a way to get the Hibernate session that already exists?

UPDATE:

Here is the exception, which makes perfect sense since there is no config file to find. Its app configured in the properties file.

org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)

For what it's worth, the code:

@NamedNativeQuery(name = "verifyEa", query = "select account_nm from per_person where account_nm = :accountName") public class VerifyEaResult { private SessionFactory sessionFact = null;

String accountName;

private void initSessionFactory()
{
    Configuration config = new Configuration().configure();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).getBootstrapServiceRegistry();

    sessionFact = config.buildSessionFactory(serviceRegistry);  
}


public String getAccountName()
{
    // Quick simple test query 
    String sql = "SELECT * FROM PER_ACCOUNT WHERE ACCOUNT_NM = 'lynnedelete'";

    initSessionFactory();

    Session session = sessionFact.getCurrentSession();


    SQLQuery q = session.createSQLQuery(sql);

    List<Object> result = q.list();

    return accountName;
}
}
6
  • Share the exception log. Commented Oct 20, 2015 at 4:52
  • or just use JDBC ... Commented Oct 20, 2015 at 6:14
  • Okay I posted the code and the exception, thanks. Commented Oct 20, 2015 at 14:55
  • It just seems to me I should be able to get either the Hibernate Session or, better, the JPA EntityManager. Thats the path I'm on, although with no success yet. If there is a better way please let me know. Commented Oct 20, 2015 at 15:45
  • already have, JDBC. Far more appropriate for what you want to do. Commented Oct 20, 2015 at 17:29

2 Answers 2

2

You can use Data access with JDBC, for example:

public class Client {
    private final JdbcTemplate jdbcTemplate;

    // Quick simple test query 
    final static String SQL = "SELECT * FROM PER_ACCOUNT WHERE ACCOUNT_NM = ?";

    @Autowired
    public Client(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public List<Map<String, Object>> getData(String name) {
        return jdbcTemplate.queryForList(SQL, name);
    }


}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I ended up doing something very similar to this. Spring provides JdbcTemplate Autowired, so there is no need to Autowire the datasource.
0

The short way is:

jdbcTemplate.queryForList("SELECT 1", Collections.emptyMap());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.