使用将其键定义为EmbeddableId的实体INSERT SELECT

时间:2017-05-22 14:49:34

标签: hibernate hql jpql

我有以下实体,其主键定义为[]

allow_null

<properties> <src.dir>src/main/java</src.dir> </properties> <profiles> <profile> <id>lombok-build</id> <properties> <src.dir>${project.build.directory}/generated-sources/delombok</src.dir> </properties> </profile> </profiles> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.8</version> <scope>provided</scope> </dependency> </dependencies> <build> <sourceDirectory>${src.dir}</sourceDirectory> <plugins> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.16.16.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> <configuration> <sourceDirectory>src/main/java</sourceDirectory> <addOutputDirectory>false</addOutputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> @EmbeddableId如下所示。

@Entity
public class TeamResult implements Serializable {
    @EmbeddedId
    private TeamMatchdayKey primaryKey;

    @Column(nullable = false)
    private long matchdayResult;

    @Column(nullable = false)
    private long afterMatchdayResult;

    // ...
}

HQL查询中使用的Embeddable实体的相关部分如下所示

TeamMatchdayKey

现在我想在JPQL中编写以下@Embeddable public class TeamMatchdayKey implements Serializable { @ManyToOne(optional = false) @PrimaryKeyJoinColumn private Team team; @ManyToOne(optional = false) @PrimaryKeyJoinColumn private Matchday matchday; //... } 查询...

Team

...但是它似乎不正确,因为启动Web应用程序时的编译会给我以下异常

@Entity
public final class Team implements Serializable {
    @Id
    @Column(length = 40)
    private String name = "";

    // ...

    @OneToMany(mappedBy="primaryKey.team", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @MapKey(name = "primaryKey.matchday")
    @OrderBy("primaryKey.matchday")
    private SortedMap<Matchday, TeamResult> teamResults = new TreeMap<>();

    // ...
}

正如您所看到的,错误消息中没有属性名称,因此我尝试使用以下内容...

INSERT ... SELECT ...

...但是这给了我以下信息

INSERT INTO TeamResult (primaryKey.team, primaryKey.matchday, matchdayResult, afterMatchdayResult)
  SELECT t.team, (SELECT MAX(m) from Matchday m), 0, COALESCE(tr.afterMatchdayResult, 0)
  FROM Team t JOIN t.teamResults tr
  WHERE tr IS EMPTY OR KEY(tr).matchdayNumber = (SELECT MAX(matchdaynumber) - 1 FROM Matchday) ORDER BY tr.afterMatchdayResult DESC, t

这条消息对我来说是可以理解的,因为我知道没有org.hibernate.QueryException: could not resolve property: of: com.example.entities.stats.TeamResult 属性,但它隐藏在我的嵌入中。

有没有办法在HQL / JPQL中为主键定义为INSERT INTO TeamResult (team, matchday, matchdayResult, afterMatchdayResult) SELECT t.team, (SELECT MAX(m) from Matchday m), 0, COALESCE(tr.afterMatchdayResult, 0) FROM Team t JOIN t.teamResults tr WHERE tr IS EMPTY OR KEY(tr).matchdayNumber = (SELECT MAX(matchdaynumber) - 1 FROM Matchday) ORDER BY tr.afterMatchdayResult DESC, t 的实体定义org.hibernate.QueryException: could not resolve property: team of: com.example.entities.stats.TeamResult 查询?

0 个答案:

没有答案
相关问题