Reputation: 13
Why am I getting error as below:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-mvc-crud-demo-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: springmvc.miniproject.entity.Review
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="reviews")
public class Review {
private int id;
private String reviews;
@ManyToMany(fetch=FetchType.LAZY,cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name="id")
private Course course;
public Review() {}
public Review(int id, String reviews) {
// super();
this.id = id;
this.reviews = reviews;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getReviews() {
return reviews;
}
public void setReviews(String reviews) {
this.reviews = reviews;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
package springmvc.miniproject.entity;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="courses")
public class Course {
@Id
@Column(name="id")
private int id;
@Column(name="Course_name")
private String courseName;
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="course_id")
private List<Review> review;
public Course() {}
public Course(int id, String courseName) {
this.id = id;
this.courseName = courseName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public List<Review> getReview() {
return review;
}
public void setReview(List<Review> review) {
this.review = review;
}
}
package springmvc.miniproject.DAO;
import java.util.List;
import springmvc.miniproject.entity.Course;
public interface CourseDAO {
public List<Course> getAllCourses();
}
package springmvc.miniproject.DAO;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springmvc.miniproject.entity.Course;
import springmvc.miniproject.entity.InstructorPersonalInfo;
import springmvc.miniproject.entity.Review;
import springmvc.miniproject.DAO.Instructor;
import springmvc.miniproject.controller.InstructorDetails;
@Repository
public class CourseDAOImpl implements CourseDAO,ReviewDAO {
@Autowired
private SessionFactory factory;
@Override
@Transactional
public List<Course> getAllCourses() {
System.out.println("getAllCourses");
//get current session
Session session = factory.getCurrentSession();
//create query
Query<Course> query = session.createQuery("from Course", Course.class);
//execute query
List<Course> course = query.getResultList();
if (course == null) {
System.out.println("null returned from query");
} else {System.out.println(course);}
return course;
}
@Override
public List<Review> getReviewForACourse(int courseId) {
// TODO Auto-generated method stub
return null;
}
}
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="springmvc.miniproject" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hb_tution_tracker?useSSL=false&serverTimezone=UTC" />
<property name="user" value="hbstudent" />
<property name="password" value="hbstudent" />
<!-- these are connection pool properties for C3P0 -->
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="springmvc.miniproject.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
</beans>```
Upvotes: 1
Views: 1766
Reputation: 10082
org.hibernate.AnnotationException: No identifier specified for entity: springmvc.miniproject.entity.Review
Looks like you are missing @Id
annotation on Review
entity
Upvotes: 1