1
votes

I am deploying a web service which uses JPA including Persistence.xml. As the persistence.xml file is inside the src folder, the war file created using the build.xml does not include the persistence.xml inside the jar which it creates before finally creating the war file.

The web sevice client complains of "No Peristence Provider for EntityManager named xxx"

Is there an option of replacing the persistence.xml with another code (probably without using Spring)

1
Why don't you fix your build?JB Nizet
The persistence.xml file needs to be added in the jar file whereas I was adding it in the war file. The build.xml does not include the persistence.xml file when it creates the jar.Sitiishh Hkar
So, you've fixed your build and everything works fine now? If so, then delete the question. If not, then fix your build so that it adds the persistence.xml to the jar file. And don't post code in comments.JB Nizet
It seems you know that the solution is to add the file to the jar. So, why don't you do it? Do it manually first. See if it fixes the problem. Then if it does, fix your build.xml so that it adds the persistence.xml fileto the jar it generates.JB Nizet
Stop adding code to comments. It's unreadable. If your question is "how to fix my ant build.xml so that persistence.xml is added to the jar", then edit your question, and ask that, clearly, showing your build.xml in your question, and telling what you tried.JB Nizet

1 Answers

0
votes

You can configure Hibernate without using persistence.xml at all in Spring like like this:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.schema-generation.database.action",
"none");
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect"); //you can change this if you have a different DB
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(adapter);
factory.setDataSource(this.springJpaDataSource());
factory.setPackagesToScan("package name");
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
factory.setJpaPropertyMap(properties);
return factory;
}

Since you are not using persistence.xml, you should create a bean that returns DataSource which you specify in the above method that sets the data source:

@Bean
public DataSource springJpaDataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:mysql://localhost/SpringJpa");
dataSource.setUsername("tomcatUser");
dataSource.setPassword("password1234");
return dataSource;
}

Then you use @EnableTransactionManagement annotation over this configuration file. Now when you put that annotation, you have to create one last bean:

@Bean
public PlatformTransactionManager jpaTransactionManager()
{
return new JpaTransactionManager(
this.entityManagerFactoryBean().getObject());
}

Now, don't forget to use @Transactional Annotation over those method that deal with DB.

Lastly, don't forget to inject EntityManager in your repository (This repository class should have @Repository annotation over it).