我是春天的新手,正在尝试一些事情,
https://spring.io/guides/gs/accessing-data-jpa/#scratch
我按照上面的教程,所以现在我有:
客户实体:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="clients")
public class ClientEntity {
@Id
@Column(name="_id")
private long id;
@Column(name="name")
private String name;
@Override
public String toString(){
return "["+this.id+"] ["+this.name+"]";
}
}
和ClientRepository:
@Repository
public interface ClientRepository extends CrudRepository<ClientEntity, Long> {}
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="relational-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>sport.center.terminal.entities.ClientEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:C:\\TerminalData\\terminal_database.db" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
JpaConfiguration:
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories("sport.center.terminal.repositories")
public class JpaConfiguration {
// @Bean
public Map<String, Object> jpaProperties() {
Map<String, Object> props = new HashMap<String, Object>();
// <!-- logging -->
props.put("javax.persistence.jdbc.driver", "org.sqlite.JDBC");
props.put("javax.persistence.jdbc.url", "jdbc:sqlite:C:\\TerminalData\\terminal_database.db");
props.put("eclipselink.logging.level", "ALL");
props.put("eclipselink.ddl-generation", "create-tables");
return props;
}
@Bean(name = "entityManagerFactory")
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
emf.setJpaPropertyMap(this.jpaProperties());
emf.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver());
emf.afterPropertiesSet();
return emf.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
}
clientRepo始终为null:
class Test {
@Autowired
ClientRepository clientRepo;
void doTest() {
for (ClientEntity client : clientRepo.findAll())
System.out.println(client);
}
}