不同数据库中的类不是实体错误。

huangapple go评论84阅读模式
英文:

Not an entity error for classes in different databases

问题

以下是您要翻译的内容:

"I work on a spring boot project which works on different DBs.
Today I tried to fetch some filtered records via Specification.
Filtering is based on a different entity on another DB with no mapping or relation.
I got a Not an entity error while fetching records. I thought the reason was based on the databases, so I tried the same way on two classes in the same DB and it worked.

My code is like that:

class 1:

@Data
@Entity
@Table(name="s")
public class S {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    Integer id;

    @Column(name="s_id")
    Integer sId;

    @Column(name="c_id")
    Integer cId;
}

class 2:

@Data
@Entity
@Table(name = "c")
public class C {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;
}

Specification class:

public class SSpecification {

    public static Specification<S> isNameLike(String nameQuery) {
        return (root, query, builder) -> {

            query.distinct(true);

            Root<C> cRoot = query.from(Co.class);

            List<Predicate> predicateList = new ArrayList<>();
            predicateList add(builder.equal(root.get("cId"), cRoot .get("id")));
            predicateList.add(builder.like(cRoot .get("name"), "%" + nameQuery + "%"));

            return builder.and(predicateList.toArray(Predicate[]::new));
        };
    }

    public static Specification<S> isMember(Boolean isMember) {
        return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("isMember"), isMember);
    }
}

Do you have any idea to solve this problem?

Ps, I thought chained Tx management could be a solution but I was wrong."

英文:

I work on a spring boot project which works on different DBs.
Today I tried to fetch some filtered records via Specification.
Filtering is based on a different entity on another DB with no mapping or relation.
I got a Not an entity error while fetching records. I thought the reason was based on the databases, so I tried the same way on two classes in the same DB and it worked.

My code is like that:

class 1:

@Data
@Entity
@Table(name="s")
public class S {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    Integer id;

    @Column(name="s_id")
    Integer sId;

    @Column(name="c_id")
    Integer cId;
}

class 2:

@Data
@Entity
@Table(name = "c")
public class C {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;
}

Specificatin class:

public class SSpecification {

    public static Specification<S> isNameLike(String nameQuery) {
        return (root, query, builder) -> {

            query.distinct(true);

            Root<C> cRoot = query.from(Co.class);

            List<Predicate> predicateList = new ArrayList<>();
            predicateList.add(builder.equal(root.get("cId"), cRoot .get("id")));
            predicateList.add(builder.like(cRoot .get("name"), "%" + nameQuery + "%"));

            return builder.and(predicateList.toArray(Predicate[]::new));
        };
    }

    public static Specification<S> isMember(Boolean isMember) {
        return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("isMember"), isMember);
    }
}

Do you have any idea to solve this problem?

Ps, I thought chained Tx management could be a solution but I was wrong.

答案1

得分: 0

除非你的数据库将它们呈现为同一数据库,否则无法使用JPA跨数据库进行查询。例如,你可以使用Oracle数据库链接来实现这一点。我预计其他大型商用数据库也会具有类似的功能。

如果没有这种数据库特性,你只能一次查询一个数据库的数据。

英文:

You can't do queries across databases with JPA except when your database presents them as if they are in the same database. You can do that for example with Oracle database links. I expect the other big commercial databases to have similar features.

Without such a database feature you can only query data from one database at a time.

huangapple
  • 本文由 发表于 2023年3月7日 18:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定