gpt4 book ai didi

jpa - Spring JPA java.lang.IllegalStateException : No persistence units parsed from {classpath*:META-INF/persistence. xml}

转载 作者:行者123 更新时间:2023-12-03 20:32:08 24 4
gpt4 key购买 nike

我正面临 Spring JPA 的以下问题。我已经完成了没有 xml 的 java 配置。

我收到“java.lang.IllegalStateException:没有从 {classpath*:META-INF/persistence.xml} 解析的持久性单元”

下面是我的应用程序配置类。

package co.in.desertlamp.configuration;

import java.util.Properties;

import javax.annotation.Resource;
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.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "co.in.desertlamp")
@ComponentScan({ "co.in.desertlamp" })
@PropertySource(value = { "classpath:/co/in/desertlamp/resources/desertlamp.properties" })
public class DesertLampApplicationConfig {

private static final String DATABASE_DRIVER = "db.driver";
private static final String DATABASE_URL = "db.url";
private static final String DATABASE_USERNAME = "db.username";
private static final String DATABASE_PASSWORD = "db.password";

private static final String HIBERNATE_DIALECT = "hibernate.dialect";
private static final String HIBERNATE_SHOWSQL = "hibernate.show_sql";
private static final String HIBERNATE_FORMATSQL = "hibernate.format_sql";
private static final String HIBERNATE_HBM2DDL = "hibernate.hbm2ddl.auto";

@Resource
private Environment environment;

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

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty(DATABASE_DRIVER));
dataSource.setUrl(environment.getRequiredProperty(DATABASE_URL));
dataSource.setUsername(environment.getRequiredProperty(DATABASE_USERNAME));
dataSource.setPassword(environment.getRequiredProperty(DATABASE_PASSWORD));
return dataSource;
}

@Bean
@Conditional(DataSourceCondition.class)
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
//Put Logger here
resourceDatabasePopulator.addScript(new ClassPathResource("/co/in/desertlamp/scripts/loadUserAndRoleData.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}

private Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.put(HIBERNATE_DIALECT, environment.getRequiredProperty(HIBERNATE_DIALECT));
hibernateProperties.put(HIBERNATE_SHOWSQL, environment.getRequiredProperty(HIBERNATE_SHOWSQL));
hibernateProperties.put(HIBERNATE_FORMATSQL, environment.getRequiredProperty(HIBERNATE_FORMATSQL));
hibernateProperties.put(HIBERNATE_HBM2DDL, environment.getRequiredProperty(HIBERNATE_HBM2DDL));
return hibernateProperties;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(sessionFactory);
return hibernateTransactionManager;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
return localContainerEntityManagerFactoryBean;
}
}

提前致谢。

最佳答案

您的 LocalContainerEntityManagerFactoryBean需要知道您的位置 @Entity类。

所以添加以下属性应该可以解决它:

localContainerEntityManagerFactoryBean.setPackagesToScan("path.to.your.entities");
localContainerEntityManagerFactoryBean.setPersistenceUnitName("name");

关于jpa - Spring JPA java.lang.IllegalStateException : No persistence units parsed from {classpath*:META-INF/persistence. xml},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37496293/

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