【问题标题】:JPA Exception- Can not find constructor for <Class> with argument types "[class java.lang.String, class java.lang.String]" to fill dataJPA 异常 - 找不到具有参数类型“[class java.lang.String, class java.lang.String]”的 <Class> 的构造函数来填充数据
【发布时间】:2015-05-21 12:43:44
【问题描述】:

我使用 jBoss Fuse 6.1.0 和带有 openJPA 的蓝图 DSL。到目前为止,我使用容器管理事务 (JTA) 和由 Aspects 管理的事务处理提交和回滚

我有以下属于 JPA 实体的类。

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

    //Had to add this for avoiding exception. And it works as expected
    //Dummy constructor for JPA - Workaround
    public Client(String s1, String s2){}

    @Column(name="requestid", unique=true,nullable=false)
    private String requestId;

    @Id
    @Column(name="clientid", unique=true, nullable=false, length=128)
    private String clientId;


    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="REQUESTID", nullable=false)
    private RoccoRequest roccoRequest;

    //bi-directional One-To-Many association to ClientGroup
    @OneToMany(mappedBy="client",fetch=FetchType.LAZY)
    private List<ClientGroup> clientGroups;
....
,...
...
}

@Entity
@Embeddable
@Table(name="CLIENTGROUP")
@NamedQuery(name="ClientGroup.findAll", query="SELECT c FROM ClientGroup c")
public class ClientGroup implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ClientGroupPK id;

    @Column(length=32)
    private String type;

    @Column(name="clientid", length=128)
    private String clientId;

    //bi-directional many-to-one association to Client
    @ManyToOne(fetch=FetchType.EAGER)
    @MapsId("clientid")
    @JoinColumn(name="CLIENTID", nullable=true, insertable=false,  updatable=false)
    private Client client;
..
.
.
.
}

@Entity
@Table(name="ROCCOREQUEST")
@NamedQuery(name="RoccoRequest.CHECK_EXISISTING_CLIENT_DETAILS",
            query="SELECT r FROM RoccoRequest r JOIN r.client c WHERE c.crmId = :crmId")
public class RoccoRequest implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="requestid", unique=true, nullable=false, length=128)
    private String requestId;

@OneToOne(mappedBy="roccoRequest", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE})
    private Client client;
..
..
..

CriteriaQuery<Client> criteriaQuery =  criteriaBuilder.createQuery(Client.class);
            Root<Client> clientRoot =  criteriaQuery.from(Client.class);
            //Join the Client table with the RoccoRequest table
            final Join<Client, RoccoRequest> clientRoccoJoin = clientRoot.join(Client_.roccoRequest,JoinType.INNER); 

            final Path<String> _requestStatus = clientRoccoJoin.get(RoccoRequest_.statusCode);
            final Path<String> _requestId = clientRoccoJoin.get(RoccoRequest_.requestId);

            final Predicate _crmIdPredicate = criteriaBuilder.equal(clientRoot.get(Client_.crmId), CRMId);

            criteriaQuery.multiselect(_requestId,_requestStatus);
            criteriaQuery.where(_crmIdPredicate);

            //Get list of details of existing requests for the client with the request type as ACO
            clientDetails = entityManager.createQuery(criteriaQuery).getResultList();

            if(null != clientDetails) for(Client clientDetail : clientDetails){
                StatusBO statusDetails = new StatusBO();
                    statusDetails.setCode((clientDetail.getRoccoRequest().getStatusCode()));
                PreInitiationBO preinitiateDetails = new PreInitiationBO();
                    preinitiateDetails.getCaseHeader().setRequestId(requestId);
                    preinitiateDetails.setStatus(statusDetails);
                exisitngRequestInfo.add(preinitiateDetails);    
            }

我已经对实体进行了一些条件提取。但我得到一个例外如下:

找不到“class com.xxx.xxx.model.Client”的构造函数 参数类型“[class java.lang.String, class java.lang.String]”到 填写数据。

为什么 JPA 需要一个参数构造函数?跟协会有关系吗?我尝试删除 OneToMany 关系,但仍然收到错误消息。

请注意,我添加了一个对我来说毫无意义的 2 参数构造函数。但如果给出它,它会起作用。日志根级别已启用调试。它包含的异常信息非常少。

请帮忙。

【问题讨论】:

  • 当你得到它时你正在执行什么操作?发布任何异常的堆栈跟踪都会揭示你在做什么。
  • @Neil,感谢您的回复。尽管我尝试这样做,但无法打印异常堆栈跟踪。该应用程序是一个 OSGi 捆绑包,我已启用调试。我使用 JPA Criterias 执行 Fetch with join 以进行过滤,该过滤在 Client.class 上运行,它给出了 exception.Debug 跟踪 <1540826 nonfatal user error>

标签: jpa openjpa


【解决方案1】:

正如 JBNizet 指出的那样, 我通过添加带有两个字符串的多选而犯了一个愚蠢的错误,但是有一个 Client.class 类型的 CrtieriaQuery。

这可以通过删除多选来解决(在我的情况下不是)或通过使用 Tuples.class 而不是 Client.class 制作 CriteriaQuery 和其他类型并循环遍历元组并获取为 tuple.get(0) 等.

问题已解决。谢谢@Neil 和@JBNizet

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 2023-01-25
    • 1970-01-01
    • 2023-01-17
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多