标签: spring-data-jpa

Spring JPA 抛出“未知实体”异常

Jdk 1.8.0_82、Springboot 1.3.8 和 JPA。

我想插入由现有对象值组成的新行。

我只是打电话

List<LogSchemaFieldModel> newFields = Lists.newArrayList();
for(LogSchemaFieldModel f : fields){
newFields.add(new LogSchemaFieldModel(){{
                        setFieldName( f.getFieldName() );
                        setFieldType( f.getFieldType() );
                        setFieldOpt( Field.Mode.NULLABLE.toString() );
                        setDescription( f.getDescription() );
                        setSampleValue( f.getSampleValue() );
                        setCommon(true);
                        setRequired(null);
                    }});
}
repo.save(newFields);
Run Code Online (Sandbox Code Playgroud)

但它抛出了这个异常。

org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: com......service.LogDefineService$6; nested exception is java.lang.IllegalArgumentException: Unknown entity: com......service.LogDefineService$6
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384) ~[spring-orm-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:227) ~[spring-orm-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436) ~[spring-orm-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) ~[spring-data-jpa-1.9.5.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at …
Run Code Online (Sandbox Code Playgroud)

java-8 spring-data-jpa

0
推荐指数
1
解决办法
1328
查看次数

没有类型为“org.springframework.data.jpa.repository.support.JpaEntityInformation”的合格 bean

我正在尝试通过 BaseRepository 接口和 BaseRepositoryImpl 扩展 **SimpleJpaRepository **。
\nTha BaseRepositoryImpl 扩展 **SimpleJpaRepository ** 并实现 BaseRepository。

\n\n

此外,我还有一些其他存储库,例如扩展了 BaseRepository 的 CandidateRepository 和 EmployeeRepository。

\n\n

下面您可以看到错误后的代码:

\n\n

要查看项目结构,请单击此处

\n\n
    @NoRepositoryBean  \n    public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>{\n//..\n}\n\n\n@Repository\npublic abstract class BaseRepositoryImpl<T, ID extends Serializable> extends \n                                                    SimpleJpaRepository<T, ID> implements BaseRepository<T, ID>{\n\n    private final EntityManager em;\n\n    public BaseRepositoryImpl(JpaEntityInformation<T,ID> entityInformation, \n                                EntityManager entityManager) {\n\n      super(entityInformation, entityManager);\n      this.em = entityManager;\n\n  }\n  //..\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

扩展存储库:

\n\n
@NoRepositoryBean\n    public interface CandidateRepository extends BaseRepository<DatCandidate, UUID>{\n\n        //..\n    }\n\n\n@Repository(value="CandidateRepositoryImpl")\npublic …
Run Code Online (Sandbox Code Playgroud)

java spring repository entitymanager spring-data-jpa

0
推荐指数
1
解决办法
8164
查看次数

Spring Boot 数据源和 h2 数据库路径

在我的 Spring Boot 应用程序中,我尝试配置 H2 数据库文件夹的路径。我想通过以下路径放置它:

/home/public/h2
Run Code Online (Sandbox Code Playgroud)

配置如:

# Datasource
spring.datasource.url=jdbc:h2:file:/home/public/h2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Run Code Online (Sandbox Code Playgroud)

导致以下错误:

Caused by: org.h2.jdbc.JdbcSQLException: A file path that is implicitly relative to the current working directory is not allowed in the database URL "jdbc:h2:file:/home/public/h2". Use an absolute path, ~/name, ./name, or the baseDir setting instead. [90011-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357) ~[h2-1.4.197.jar:1.4.197]
Run Code Online (Sandbox Code Playgroud)

我也尝试过spring.datasource.url=jdbc:h2:file:~/home/public/h2 ,但没有成功。

我做错了什么以及如何正确配置路径?

datasource h2 spring-data spring-data-jpa spring-boot

0
推荐指数
1
解决办法
1万
查看次数

Spring Data REST 投影值的默认值?

我配置了 Spring Data REST 投影,当可以找到相关数据时,它会按预期工作。但是当没有找到相关数据时,会抛出以下错误:

{
"timestamp": 1532021102433,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON document: EL1007E: Property or field 'model' cannot be found on null (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.Collections$UnmodifiableMap[\"processed\"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionResource[\"content\"]->com.sun.proxy.$Proxy122[\"model\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: EL1007E: Property or field 'model' cannot be found on null (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.Collections$UnmodifiableMap[\"processed\"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionResource[\"content\"]->com.sun.proxy.$Proxy122[\"model\"])",
"path": "/processed/search/findByCompCode"
}
Run Code Online (Sandbox Code Playgroud)

我的投影界面如下所示:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.config.Projection;

import java.util.Date;

@Projection(name = "usage-detail", types = {Usage.class})
interface UsageProjection {
String getUuid();
String getProduct();
Date getReportDate();
String …
Run Code Online (Sandbox Code Playgroud)

spring spring-data-jpa spring-data-rest

0
推荐指数
1
解决办法
2718
查看次数

如何使用带注释的查询让 PageRequest 在 Spring Boot 中工作

我正在从 Spring Boot 1 升级到 Spring Boot 2。

我只想挑选第一个结果。

PageRequest pageRequest = PageRequest.of(0, batchSize, Sort.Direction.ASC, "id");
Run Code Online (Sandbox Code Playgroud)

在存储库上使用带注释的查询

@Query("select c from Component")
List<Component> findReturnComponent(PageRequest pageRequest);
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息

but parameter 'Optional[pageRequest]' not found in annotated query 'select c from Component
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data-jpa spring-boot

0
推荐指数
1
解决办法
3376
查看次数

Spring boot:违反引用完整性约束

您好,我遇到了这个错误,我找到的解决方案都不起作用。我有一个购物车实体:

@Data
@Entity
public class Cart {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String status;
  @ManyToOne(cascade = CascadeType.MERGE)
  @JoinColumn(name = "usr_id", nullable = false)
  private User usr;
  @ElementCollection(targetClass=String.class)
  private List<String> productsBarcode = new ArrayList<>();
  private long nutritional_score;
}
Run Code Online (Sandbox Code Playgroud)

和一个 User 实体

@Entity
@Data
public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String firstname;
    private String lastname;
    private String email;
    private String phone;
    @JsonIgnore
    @OneToMany(mappedBy = "id",cascade=CascadeType.ALL)
    private List<Cart> carts = new ArrayList<Cart>();
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下 JSON 传递 …

hibernate-mapping spring-data-jpa spring-boot

0
推荐指数
1
解决办法
1万
查看次数

Spring Data JPA:java.sql.SQLException:未找到列“id”

尝试从名为的列返回列表make会导致控制台中id找不到列的错误。指定的列id位于表中,但不确定为什么无法识别它。

汽车.java

 package com.example.demo;


import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Car {

    @Id
    private String id;

    private String make;
    private String model;
    private String year;


    public Car() {
    }

    public Car(String id, String make, String model, String year) {
        this.id = id;
        this.make = make;
        this.model = model;
        this.year = year;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMake() {
        return …
Run Code Online (Sandbox Code Playgroud)

spring-data-jpa spring-boot

0
推荐指数
1
解决办法
5371
查看次数

Spring Data JPA,查询太慢,子查询太多而不是join

我有一个使用 Spring Data JPA 调用简单的 REST 服务findAll。问题是在控制台中记录了很多查询,因此我认为 Spring Data 不是对我的主类的每一行进行一次查询,而是对名为 的主类进行一次查询Dis

我有一个主要实体,它必须仅获取一些用以下注释的数据@JsonView

@Entity
@NamedQuery(name="Dis.findAll", query="SELECT d FROM Dis d")
public class Dis implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonView(View.Summary.class)
    @Id
    @Column(name="id_dis")
    private Integer idDis;

    private byte[] logo;

    @JsonView(View.Summary.class)
    private String text;

    @JsonView(View.Summary.class)
    @JsonManagedReference
    //bi-directional many-to-one association to TipoValidazione
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="validazione")
    private TipoValidazione tipoValidazione;

    //bi-directional many-to-one association to DisComune
    @OneToMany(mappedBy="dis")
    private List<DisComune> disComunes;

    //bi-directional many-to-one association to PdfDis
    @OneToMany(mappedBy="dis")
    private List<PdfDis> …
Run Code Online (Sandbox Code Playgroud)

java spring-data-jpa spring-boot

0
推荐指数
1
解决办法
3488
查看次数

@事务回滚在 Spring Boot 应用程序中不起作用?

我有一个要求,我需要将一些员工数据插入 db2 数据库,下面是代码:

@Log4j2
@Service
@Transactional
public class EmployeeServiceImpl extends EmployeeService{

@Autowired
private EmployeeDataRepository employeeDataRepository;

@Override
@Transactional(rollbackOn = {DataAccessException.class})
public void insertEmployeeData(List<Employee> employeeList){
 try{
     employeeDataRepository.insertEmployeeData(employeeList);
    }catch(DataAccessException ex){
      log.error("Exception whie inserting data in db {}",ex.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)
@Log4j2
@Repository
public class EmployeeDataRepositoryImpl implements EmployeeDataRepository{

@Autowired
private DataSource dataSource;

@Value("${db.schema}")
private String schema;

@Override
public void insertEmployeeData(List<Employee> employeeList){
   this.jdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("Employee");
   for(Employee emp : employeeList){
    Map<String,Object> parameters = new HashMap<String,Object>();
      parameters.put("name", emp.name);
      parameters.put("age", emp.age);
      parameters.put("dateOfJoining", emp.dateOfJoining);
      parameters.put("address", emp.address);
      parameters.put("salary", emp.salary);
      jdbcInsert.execute(parameters); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data spring-data-jpa spring-boot

0
推荐指数
1
解决办法
3209
查看次数

无法在此 ManagedType 上找到具有给定名称的属性 [未知]

我有一个名为

public class Customer implements Auditable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "SellerID")
private Long id;

@Embedded
private AuditSection auditSection = new AuditSection();
......
......
}

@Embeddable
public class AuditSection implements Serializable {
private static final long serialVersionUID = -1934446958975060889L;
@Temporal(TemporalType.TIMESTAMP)
  @Column(name = "DateCreated")
  private Date dateCreated;

  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "DateModified")
  private Date dateModified;
  ......
  }
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 criteriaBuilder 和谓词进行搜索

 predicates.add(criteriaBuilder.and(criteriaBuilder.notEqual(root.get("modifiedDate"), new Date)));
Run Code Online (Sandbox Code Playgroud)

在这里我得到了异常

org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate Attribute  with the the given name [modifiedDate] on this ManagedType [unknown]; nested …
Run Code Online (Sandbox Code Playgroud)

hibernate criteria-api spring-data-jpa

0
推荐指数
1
解决办法
1万
查看次数