I am working on a spring boot project in which I have entities with @Entity Decorator which do not have @Id decorator because they are views and not tables. this is why I have a persistence.xml file which says that the classes processed hibernate here
package com.lma.flad;
@Entity
class ClassA{
private String name;
private String surName
}
package com.lma.flad;
@Entity
class ClassB{
@Id
private Long id
private String name;
private String surName
}
package com.lma.flad;
@Entity
class ClassC{
@ID
private Long id
private String name;
private String surName
}
persistance.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="dtmowliance">
<class>com.lma.flad.ClassB</class>
<class>com.lma.flad.ClassC</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
</properties>
</persistence-unit>
</persistence>
in this case, hibernate will only analyze the classes found in the persistence xml file (classA and Class B)
How i can do the persistence.xml with java config in spring boot
Since you have created the Entity classes (JPA), you have to follow the subsequest steps:
Imports: Common for 3 Entities
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Repository for each Entity
@Repository
public interface ClassARepository extends JpaRepository<ClassA, Long> {
}
@Repository
public interface ClassBRepository extends JpaRepository<ClassB, Long> {
}
@Repository
public interface ClassCRepository extends JpaRepository<ClassC, Long> {
}
application.properties
file to connect database to JPA code. spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2-console
#Turn Statistics on and log SQL stmts
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create-drop
#If want to see very extensive logging
spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.stat=debug
3.Spring Boot to test your connectivity with database and JPA
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import <your-path>.entity.ClassA;//change accordingly
import <your-path>.entity.ClassB;//change accordingly
import <your-path>.entity.ClassA;//change accordingly
import <your-path>.repository.ClassARepository;// change accordingly
import <your-path>.repository.ClassBRepository;// change accordingly
import <your-path>.repository.ClassCRepository;// change accordingly
@SpringBootApplication
public class DB2JPAApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
ClassARepository classArepository;
@Autowired
ClassBRepository classBrepository;
@Autowired
ClassCRepository classCrepository;
public static void main(String[] args) {
SpringApplication.run(DB2JPAApplication.class, args);
}
@Override
public void run(String... args) throws Exception
{
Optional<ClassAEntity> classC = repository.findById(2L);
logger.info("ClassC id 2 -> {}", classC.get());
Optional<ClassAEntity> classB = repository.findById(2L);
logger.info("ClassB id 2 -> {}", classB.get());
}
NOTE:
Your classA
has no @Id. JPA makes it mandatory to have an ID coloumn.
Change Entity and Reppsitory imports in DB2JPAApplication
class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.