-1

我正在使用 Spring 3.2 和 Hibernate,但我发现它不允许从 XML 文件创建 SessionFacotry bean 我不知道为什么会出现这个错误。我搜索了很多,但没有找到 exceat ans 请帮助。

这里我的文件是 XML

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!-- For context suppport of the Controller -->

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


    <!-- For enable the MVC Support -->
    <!-- <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"></mvc:resources>
 -->
    <!-- Enabling the suppot the UI user side -->
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>




    <!-- For messages for the Labels -->

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>


    <!-- For the Properties of JDBC -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <!--Database Properties -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <!-- Session facotry configration -->
    <bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property> -->
        <property name="packagesToScan" value="com.book.bookentity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- implementation bean config. -->

    <bean id="autherMasterServiceInterfacecontroller" class="com.book.service.AutherMasterServiceInterfaceImpl"></bean>
    <bean id="bookMasterServiceInterfacecontroller" class="com.book.service.BookMasterServiceInterfaceImpl"></bean>
    <bean id="emailMasterInterfacecontroller" class="com.book.service.EmailMasterInterfaceImpl"></bean>
    <bean id="languageMasterinterfacecontroller" class="com.book.service.LanguageMasterinterfaceimpl"></bean>




    <bean id="bookMasterInterface" class="com.book.bookDao.BookMasterInterfaceImpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>
    <bean id="emailMasterInterface" class="com.book.bookDao.EmailMasterInterfaceImpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>
    <bean id="languageMasterInterface" class="com.book.bookDao.LanguageMasterInterfaceImpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>
    <bean id="autherMasterInterface" class="com.book.bookDao.AutherMasterInterfaceImpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>



    <!-- Enabling transction managar -->
    <tx:annotation-driven transaction-manager="transactionManager" />


    <!-- Config Tranaction Managar -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>


</beans>

豆类1

@Entity
@Table(name = "tbl_bookmaster", uniqueConstraints = { @UniqueConstraint(columnNames = "bid") })
public class BookMasterTable {

    @Id
    @GeneratedValue
    @Column(name = "bid")
    private int bookid;

    @Column(name = "bookname")
    private String bookname;

    @Column(name = "booktype")
    private String booktype;

    @Column(name = "bookprice")
    private String bookprice;

    @Column(name = "bestsaler")
    private int bestsaller;

    @ManyToOne
    @JoinColumn(name = "lid")
    private LanguageMasterTable languageMasterTable;

    @ManyToOne
    @JoinColumn(name = "auid")
    private AutherMasterTable autherMasterTable;

    @ManyToOne
    @JoinColumn(name = "mailid")
    private EmailMasterTable emailMasterTable;


    // getter and setter

    }

豆2

@Entity
@Table(name = "tbl_contactmaster")
public class EmailMasterTable implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @OneToMany
    @JoinColumn(name = "authercontactmail")
    private Set<BookMasterTable> bookMasterTables;

    @Column(name = "emailcontact")
    private String emailcontact;

}

豆 3

@Entity
@Table(name = "tbl_authermaster")
public class AutherMasterTable {

    @Id
    @GeneratedValue
    @OneToMany
    @JoinColumn(name = "auid")
    private Set<BookMasterTable> bookMasterTables;

    @Column(name = "authername")
    private String authername;
   // getter and setter
}

豆 4

public class LanguageMasterTable {

    @Id
    @GeneratedValue
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "language")
    private Set<BookMasterTable> bookMasterTables;

    @Column(name = "languagename")
    private String languagename;

    // getter and setter
}

控制器豆

@Controller
public class BookMasterController {

    @Autowired
    private BookMasterServiceInterface bookMasterServiceInterfacecontroller;


    @RequestMapping(value="/addbook",method= RequestMethod.POST)
    public String addAuther(@ModelAttribute("book") BookMasterTable bookMasterTable){

        bookMasterServiceInterfacecontroller.addBook(bookMasterTable);
        return "bookadded";
    }
}

就像每个 bean 类都有所有控制器

服务接口

public interface BookMasterServiceInterface {
    public void addBook(BookMasterTable bookMasterTable);
}

服务类

@Service
public class BookMasterServiceInterfaceImpl implements BookMasterServiceInterface{
    @Autowired
    private BookMasterInterface bookMasterInterface;

    @Override
    @Transactional
    public void addBook(BookMasterTable bookMasterTable) {
        // TODO Auto-generated method stub
        bookMasterInterface.addBooks(bookMasterTable);

    }
}

就像为所有 bean 服务一样

存储库类接口

public interface BookMasterInterface {
    public void addBooks(BookMasterTable bookMasterTable);
}

具有每个 bean 的存储库类实现

@Repository
public class BookMasterInterfaceImpl implements BookMasterInterface {
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addBooks(BookMasterTable bookMasterTable) {
        // TODO Auto-generated method stub

        try {
            sessionFactory.getCurrentSession().save(bookMasterTable);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}

我得到的错误是。

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.book.service.BookMasterServiceInterface com.book.controller.BookMasterController.bookMasterServiceInterfacecontroller; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookMasterServiceInterfacecontroller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.book.bookDao.BookMasterInterface com.book.service.BookMasterServiceInterfaceImpl.bookMasterInterface; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookMasterInterface': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.book.bookDao.BookMasterInterfaceImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/BookServletmvc-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 20 more

主要是不允许创建,因为它得到空指针,但我不知道这里在哪里。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/BookServletmvc-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 48 more
Caused by: java.lang.NullPointerException
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:538)
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:126)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1437)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 58 more

如果你能提供帮助,我会很高兴:)

我有四个实体。第一个是

@Entity
@Table(name = "tbl_bookmaster", uniqueConstraints = { @UniqueConstraint(columnNames = "bid") })
public class BookMasterTable {

    @Id
    @GeneratedValue
    @Column(name = "bid")
    private int bookid;

    @Column(name = "bookname")
    private String bookname;

    @Column(name = "booktype")
    private String booktype;

    @Column(name = "bookprice")
    private String bookprice;

    @Column(name = "bestsaler")
    private int bestsaller;

    @ManyToOne
    @JoinColumn(name = "lid")
    private LanguageMasterTable languageMasterTable;

    @ManyToOne
    @JoinColumn(name = "auid")
    private AutherMasterTable autherMasterTable;

    @ManyToOne
    @JoinColumn(name = "mailid")
    private EmailMasterTable emailMasterTable;

    public int getBookid() {
        return bookid;
    }

    public void setBookid(int bookid) {
        this.bookid = bookid;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public String getBooktype() {
        return booktype;
    }

    public void setBooktype(String booktype) {
        this.booktype = booktype;
    }

    public String getBookprice() {
        return bookprice;
    }

    public void setBookprice(String bookprice) {
        this.bookprice = bookprice;
    }

    public int getBestsaller() {
        return bestsaller;
    }

    public void setBestsaller(int bestsaller) {
        this.bestsaller = bestsaller;
    }

    public LanguageMasterTable getLanguageMasterTable() {
        return languageMasterTable;
    }

    public void setLanguageMasterTable(LanguageMasterTable languageMasterTable) {
        this.languageMasterTable = languageMasterTable;
    }

    public AutherMasterTable getAutherMasterTable() {
        return autherMasterTable;
    }

    public void setAutherMasterTable(AutherMasterTable autherMasterTable) {
        this.autherMasterTable = autherMasterTable;
    }

    public EmailMasterTable getEmailMasterTable() {
        return emailMasterTable;
    }

    public void setEmailMasterTable(EmailMasterTable emailMasterTable) {
        this.emailMasterTable = emailMasterTable;
    }

}
4

2 回答 2

0

阅读堆栈跟踪,您知道在哪里:

java.lang.NullPointerException
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:538)
    at ...

您应该在调试中运行该行的断点,但我认为这基本上意味着您的实体、映射或运行它的数据库中有错误。如果可以,您应该使用 Hibernate 生成模式,然后您应该比较目标模式和生成的模式之间的差异 => 会打印映射错误。

mappedBy您的 OneToMany 和 ManyToOne 上也没有。您应该先检查一下(没有它们不是错误,但如果数据库模式与 Hibernate 预期的模式不同步,它可能会成为问题)。

于 2014-09-06T12:54:11.480 回答
0

我认为问题出在 AutherMasterTable 中(以及其他实体中的类似位置):

@Id
@GeneratedValue
@OneToMany
@JoinColumn(name = "auid")
private Set<BookMasterTable> bookMasterTables;

对于一对多连接,JoinColumn注释中提到的连接列在另一个表中。它不引用此表中的列。此外,aSet不能是标识符。

尝试使用两个字段:

@Id
@GeneratedValue
@Column(name = "auid")
private int autherId;

@OneToMany
@JoinColumn(name = "auid")
private Set<BookMasterTable> bookMasterTables;

请注意,我们没有在同一个表中定义同一列两次:auid上面第一次提到的是指 in 中的列,tbl_authermaster第二次指的是 中的auidtbl_bookmaster

尽管您犯了错误,但 Hibernate 不应该向您抛出 NullPointerException。这是 Hibernate 中的一个错误。

于 2014-09-06T13:53:34.280 回答