0

I am developing an application using spring boot with hibernate. Actually previously i created a project using spring mvc with hibernate but now i started working on spring boot. So the problem is that i am unable to fetch data using my hql query which i used in spring mvc. Because i dont know where to write hql queries in spring boot project.

I tried and created a class and extend spring boot dao interface and implement my method but not getting proper data.

Here is my previous spring mvc code.

I created an entity class Rating.java and a dao class. Here is my Rating.java

@Entity
@Table(name = "rating")
public class Rating {


@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name="author_id")
private long authId;

@Id
@Column(name="post_id")
private long postId;

@Column(name="rating_points")
private long points;


public Rating() { }

public Rating(long id) { 
this.id = id;
}

public Rating(long authId, long postId, long points) {
    this.authId = authId;
    this.postId = postId;
    this.points = points;
}
//Getters and setters
}

My spring mvc DAO class

    @SuppressWarnings("unchecked")
public List<Rating> getRatingInfoById(long id) throws Exception {
    session = sessionFactory.openSession();
    String queryString = "select r.id from Rating r where r.postId=:id group by points";
    Query query = session.createQuery(queryString);
    query.setLong("id", id);

    return query.list();
}

My spring boot code.

DAO Interface

@Transactional
public interface MyDaoInterface extends CrudRepository<Rating, Long>{

public List<Rating> getRatingInfoById(long id) throws Exception;

}

My DAO class where i implemented dao interface method

public class MyDao implements MyDaoInterface{


@Autowired
SessionFactory sessionFactory;

Session session = null;
Transaction tx = null;


@SuppressWarnings("unchecked")
public List<Rating> getRatingInfoById(long id) throws Exception {
    session = sessionFactory.openSession();
    String queryString = "select r.id from Rating r where r.postId=:id group by points";
    Query query = session.createQuery(queryString);
    query.setLong("id", id);

    return query.list();
}

}

1
  • Turn on debug logging for hibernate, do the task you are trying, and post debug log. Also use session = sessionFactory.getCurrentSessio(); instead of openSession(). Show annotations on your MyDao class and your controller, service class for getRating. Commented Aug 28, 2015 at 7:38

1 Answer 1

1

It looks like you use spring-data (CrudRepository) so queries should be define in the interface (MyDaoInterface).

I'm not sure that using SessionFactory with Spring-data is a good idea, it would be easier for you to use Spring-Data-Jpa (But still with hibernate for the JPA impl)

You should have a config class like this (look http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.java-config):

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
class ApplicationConfig {

  @Bean
  public DataSource dataSource() {

    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).build();
  }

  @Bean
  public EntityManagerFactory entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.acme.domain");
    factory.setDataSource(dataSource());
    factory.afterPropertiesSet();

    return factory.getObject();
  }

  @Bean
  public PlatformTransactionManager transactionManager() {

    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory());
    return txManager;
  }
}

And you have to create queries from method names, more explanation here: http://docs.spring.io/spring-data/data-commons/docs/current/reference/html/#repositories.query-methods.query-creation

If you can't do it from method names, you still can use @Query on your method interface.

Instead of extends CrudRepository you can extends JpaRepository (more generics methods available)

And just a word for @Transactional, it's better to use it on methods define in the service layer (services are calling the DAO layer, and for example if one of them is responsible to create one user and one role, if it's failing, you want a rollback for the user AND the role created!)

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

7 Comments

what is the use of this config class?
do i need service class in my project?
To define an EntityManagerFactory (JPA) instead of Hibernate SessionFactory and to define your transactionManager (to have @Transactional working)
You need services to use you repository services. (via @Autowiring)
i am unable to get you...can you please provide me any link of examples of spring boot with hql ?
|

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.