Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java iReport+;休眠连接抛出无效配置_Java_Spring_Hibernate_Jasper Reports_Ireport - Fatal编程技术网

Java iReport+;休眠连接抛出无效配置

Java iReport+;休眠连接抛出无效配置,java,spring,hibernate,jasper-reports,ireport,Java,Spring,Hibernate,Jasper Reports,Ireport,我有一个Java项目,它使用Spring+Hibernate+JasperReports生成PDF格式的报告。该报告是基于我使用MySQL JDBC连接通过iReport生成的.jrxml文件,使用JasperReports正确生成的。在Java代码中,我使用带注释的Hibernate/Spring。仅使用注释而不使用文件:hibernate.cfg.xml我可以在从MySQL数据库获取数据后创建PDF报告 所以,Java应用程序使用Hibernate可以正常工作,而iRport使用JDBC连接

我有一个Java项目,它使用
Spring
+
Hibernate
+
JasperReports
生成PDF格式的报告。该报告是基于我使用MySQL JDBC连接通过iReport生成的
.jrxml
文件,使用
JasperReports
正确生成的。在Java代码中,我使用带注释的
Hibernate/Spring
。仅使用注释而不使用文件:
hibernate.cfg.xml
我可以在从MySQL数据库获取数据后创建PDF报告

所以,Java应用程序使用Hibernate可以正常工作,而iRport使用JDBC连接可以正常工作

问题是,当我尝试使用
Hibernate连接
iReport
连接到
MySQL
数据库时,为了充分利用Hibernate功能。我想使用与Java上的
成功的
相同的Hibernate配置

正如我所看到的,iReport总是查找文件:
hibernate.cfg.xml
。然后我在
src/main/resources
(特别是为了这个目的)上创建了一个,并在构建项目后将其复制到:
\target\classes
,这是我将添加到下面的
iReport
类路径的文件夹

hibernate.cfg.xml
的全部内容如下:

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.elkiosko" />

</beans>
package com.elkiosko.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.elkiosko" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
}
用于:

PDF报告也被正确地生成了

现在转到iReport…

我已经配置了iReport的类路径,添加了文件:
hibernate.cfg.xml
所在的文件夹

当尝试在iReport上设置Hibernate连接时,我得到:

错误-配置无效

当点击测试按钮时

我还尝试检查:
使用Hibernate注释
,但没有成功

我不明白为什么在Java中Hibernate连接可以工作,但在iReport中却不行

还记得我采用的是
注释
,而不是
xml
。我创建了文件:
hibernate.cfg.xml
,只是为了让iReport知道扫描配置的包是什么

类的映射在模型类中完成,如下所示:

package com.elkiosko.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Producto {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "price", nullable = false)
    private float price;

    @Column(name = "stock", nullable = false)
    private int stock;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Producto))
            return false;
        Producto other = (Producto) obj;
        if (this == other)
            return true;
        if (this.id != other.id)
            return false;
        if (!this.name.equals(other.name))
            return false;
        if (this.price != other.price)
            return false;
        if (this.stock != other.stock)
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Producto [id=" + id + ", name=" + name +
                ", price=" + price + ", stock=" + stock + "]";
    }

}
我还实现了其他类,例如:
HibernateConfiguration.java
,它被注释为:
@Configuration
(java代码正常工作,问题在于iReport)

文件内容:
HibernateConfiguration.java
如下:

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.elkiosko" />

</beans>
package com.elkiosko.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.elkiosko" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
}
我还尝试设置了一个
弹簧加载的Hibernate连接
,但没有成功:

似乎它在
hibernate.cfg.xml
中定义了一个bean,但是我没有在那里定义任何bean,因为我在文件
hibernate配置.java
中定义了
sessionFactory
bean。无论如何,这种方式被放弃了,因为iReport使用的是旧版本的Spring,它与我使用的
Spring
版本不兼容:
4.2.0
。因此,我唯一能做的就是创建一个
Hibernate
连接

关于如何使用
iReport
配置
Hibernate
连接,有什么想法吗