gpt4 book ai didi

java - 没有定义名为 'persistenceUnit' 的 bean

转载 作者:行者123 更新时间:2023-11-28 23:56:22 26 4
gpt4 key购买 nike

我正在使用 springjpahibernatemysql 数据库开始一个新项目,但我有此配置有问题。

我开发了一个类似的架构,但我从未创建过它,我正在与之抗争。

这是我的 dispatcher-servlet.xml

<!-- Persistencia -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<!-- DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/editor" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.editor.entity.FolderEntity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

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

这是我的主要 DAO 持久性配置。

@PersistenceContext(unitName = "persistenceUnit")
private EntityManager entityManager;

我有一个正在运行的 mysql 数据库 在我的机器上运行。我的机器中有这个 persistence.xml 文件。

META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.gamecreator.editor.entity.FolderEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/gamecreator"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>

我有这个 spring 和持久性配置,以及我在 DAO 中的标签 persistenceUnit,但这不起作用。我不知道我是否以正确的方式“连接”了这些元素。

我读了很多关于这个主题的文章,但我无法让它发挥作用。有什么帮助吗?关于这个主题的每个教程都是不同的。

如果我评论我的 persistence.xml 文件,我得到同样的错误,我认为这个文件没有加载。

最佳答案

你的注释是错误的。应该是

@PersistenceContext(unitName = "gameCreatorDatabase")
private EntityManager entityManager;

关于java - 没有定义名为 'persistenceUnit' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com