我从https://spring.io/guides/gs/accessing-data-jpa/获得了Spring Data JPA入门项目。我可以创建Customer
和CustomerRepository
类,并使Customer
实体持久化。
但是,当我添加另一个实体Person
和aPersonRepository
并尝试保留该实体时,出现如下所示的错误。就像在内部Application.java
某种内部类中$1
,Spring数据试图将其映射为一个实体。
java.lang.IllegalArgumentException:未知实体:hello.Application $ 1
package hello;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@ToString
@NoArgsConstructor
@Data
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int age;
}
package hello;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, Long> {
}
package hello;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@Slf4j
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
/*
@Bean
public CommandLineRunner demo(CustomerRepository repository) {
return (args) -> {
repository.save(new Customer("Jack", "Bauer"));
repository.save(new Customer("Chole", "O'Brian"));
repository.save(new Customer("Kim", "Bauer"));
repository.save(new Customer("David", "Palmer"));
repository.save(new Customer("Michelle", "Dessler"));
// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
repository.findAll().forEach(c -> log.info(c.toString()));
};
}
*/
@Bean
public CommandLineRunner demo(PersonRepository repository) {
return (args) -> {
repository.save(new Person(){{
setAge(20);
}});
// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
repository.findAll().forEach(c -> log.info(c.toString()));
};
}
}
申请费$ 1; 嵌套异常是java.lang.IllegalArgumentException:未知实体:hello.Application $ 1,位于org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:373)〜[spring-orm-5.1.8.RELEASE.jar:5.1 .8.RELEASE]在org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)〜[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.springframework .orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)〜[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator。 java:61)〜[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE],位于org.springframework.dao.support。
该错误消息清楚地表明您具有未映射的类。你正确地扣除了
就像在Application.java中一样,Spring数据尝试将其映射为一个实体的内部类$ 1。
有问题的内部类在您的代码中定义为的子类。 Person
repository.save(new Person(){{
setAge(20);
}});
该类是一个匿名内部类,带有Instance Initializer块。请参阅什么是初始化块?
使用下面的代码创建一个新的人,而无需将其子类化:
Person p = new Person();
p.setAge(20);
repository.save(p)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句