Я пытаюсь выяснить, что не так, но я здесь бессилен
Я просто хочу сохранить сущность в базе данных, используя Hibernate SessionFactory, но получаю исключение No Session found
Версия Hibernate: 4.1 .9.Final
Версия Spring: 3.2.4.RELEASE
Вот веб. xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven />
<!-- <mvc:resources mapping="/resources/**" location="/resources/" cache-period="3600" /> -->
<context:annotation-config />
<context:component-scan base-package="com.example" /> <!-- controller -->
<!-- Load Hibernate related configuration -->
<import resource="hibernate-context2.xml" />
Hibernate- context2. xml
<context:property-placeholder location="/WEB-INF/db.properties" />
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="${hibernate.config}"
p:packagesToScan="com.example.model"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
hibernate.cfg. xml
<hibernate-configuration>
<session-factory>
<!-- We're using MySQL database so the dialect needs to MySQL as well-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable this to see the SQL statements in the logs-->
<property name="show_sql">true</property>
<!-- This will drop our existing database and re-create a new one.
Existing data will be deleted! -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
db.properties
# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/equation
app.jdbc.username=root
app.jdbc.password=12345
#hibernate properties
hibernate.config=/WEB-INF/hibernate.cfg.xml
Класс репозитория
@Repository
public class EquationRepository implements EquationDAO {
private SessionFactory sessionFactory;
@Autowired
public EquationRepository(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session currentSession() {
return sessionFactory.getCurrentSession();
}
@Override
public void addEquation(Equation equation) {
currentSession().save(equation);
}
Класс обслуживания
@Service
public class EquationServiceImpl implements EquationService {
private EquationRepository repository;
@Autowired
public void setRepository(EquationRepository repository) {
this.repository = repository;
}
@Override
public void equationCulc(EquationDto dto) {
Equation equation = DTOTransfer(dto);
double a = dto.getA();
double b = dto.getB();
double c = dto.getC();
double D = (b*b) - 4 * a * c;
if(D == 0) {
equation.setX1((-b + Math.sqrt(D)) / (2 * a));
} else if(D > 0) {
equation.setX1((-b + Math.sqrt(D)) / (2 * a));
equation.setX2((-b - Math.sqrt(D)) / (2 * a));
}
repository.addEquation(equation);
}
Класс контроллера
@Controller()
public class HomeController {
@Autowired
private EquationService equationService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showHome(Model model) {
model.addAttribute("equation", new EquationDto());
return "index";
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public String equationForm(@ModelAttribute("equation") EquationDto equationDto) {
equationService.equationCulc(equationDto);
return "redirect:/examples";
}
}
Я новичок в этом деле. Особенно с этой старой версией и xml конфиг. И простите мне это глупое название:)
Если я что-то забыл, пожалуйста, дайте мне знать