我正在用休眠和春天开发一个Web应用程序。我正在对所有配置使用批注。

我的问题是尝试选择带有两个外键的表。

包含全部的实体是:

@Entity
@Table(name = "WAR_PLAYERS_ATTACKS")
public class WarPlayerAttack implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "WAR_PLAYER")
    private WarPlayer warPlayer;

    @Id
    @ManyToOne
    @JoinColumn(name = "ATTACK")
    private Attack attack;

    @ManyToOne
    @JoinColumn(name = "TYPE_ATTACK")
    private TypeAttack typeAttackInWar;

    public WarPlayerAttack() { }

    // getters and setters
}


实体warPlayer:

@Entity
@Table(name = "WAR_PLAYERS")
public class WarPlayer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false)
    private int id;

    @ManyToOne
    @JoinColumn(name = "WAR")
    private War war;

    @ManyToOne
    @JoinColumn(name = "PLAYER")
    private Player player;

    @Column(name = "WAR_NUMBER")
    private int warNumber;

    @Column(name = "HAVE_KING")
    private boolean haveKing;

    @Column(name = "HAVE_QUEEN")
    private boolean haveQueen;

    @Column(name = "HAVE_POTIONS")
    private boolean havePotions;

    @OneToMany(mappedBy = "warPlayer", fetch = FetchType.EAGER)
    private List<WarPlayerAttack> attacks;

    public List<WarPlayerAttack> getAttacks() {
        return attacks;
    }

    public void setAttacks(List<WarPlayerAttack> attacks) {
        this.attacks = attacks;
    }

    private String comment;

    public WarPlayer() {}


我的DAO

    @Override
    public List<WarPlayerAttack> findByIdEnemyAndIdWar(int idEnemy, int idWar) throws DaoException {

        List<WarPlayerAttack> attackss = null;
        List attacks;

        try {
            attackss = getSession()
                .createQuery("from WarPlayerAttack wpa where wpa.attack.enemy.id in :idEnemy and wpa.attack.enemy.war.id in :idWar")
                .setParameter("idEnemy", idEnemy)
                .setParameter("idWar", idWar)
                //.createSQLQuery("select * from WAR_PLAYERS_ATTACKS where ATTACK in (select ID from ATTACKS where ENEMY in (select ID from WAR_ENEMIES where ID = ? and WAR = ?))")
                //.setParameter(0, idEnemy)
                //.setParameter(1, idWar)
                .list();
        } catch (Exception e) {
            throw new DaoException(e.getMessage());
        }

        return attackss;
    }


我的问题是休眠不收集warPlayer。仅收集AttackTypeAttack
以JSON收集数据的示例(出现攻击但没有warPlayer):

[
    {"attack":{"id":15,"enemy":{"id":10,"diffculty":0,"warNumber":2},"stars":1,"strategy":{"id":1,"value":"Air"},"timeRange":{"id":1,"value":"6H"}},"typeAttackInWar":{"id":3,"value":"Final 1"}},
    {"attack":{"id":16,"enemy":{"id":10,"diffculty":0,"warNumber":2},"stars":2,"strategy":{"id":2,"value":"Ground"},"timeRange":{"id":3,"value":"18H"}},"typeAttackInWar":{"id":4,"value":"Final 2"}}
]


如果我使用SQL,则dao会给我一个Object[]之类的[[1,1,1],[1,2,1]]

有关更多信息的表:

CREATE TABLE WAR_ENEMIES (
    ID INT AUTO_INCREMENT NOT NULL,
    WAR INT NOT NULL,
    WAR_NUMBER INT(2) NOT NULL,
    DIFFCULTY INT(1),

    CONSTRAINT PK_ENEMY PRIMARY KEY (ID),
    CONSTRAINT FK_ENEMY_WAR FOREIGN KEY (WAR) REFERENCES WARS(ID)
);

CREATE TABLE ATTACKS (
    ID INT AUTO_INCREMENT NOT NULL,
    ENEMY INT,
    STARS INT(1) DEFAULT 0,
    STRATEGY INT,
    TIME_RANGE INT,

    CONSTRAINT PK_ATTACK PRIMARY KEY (ID),
    CONSTRAINT FK_ATTACK_ENEMY FOREIGN KEY (ENEMY) REFERENCES WAR_ENEMIES(ID),
    CONSTRAINT FK_ATTACK_STRATEGY FOREIGN KEY (STRATEGY) REFERENCES STRATEGIES(ID),
    CONSTRAINT FK_ATTACK_TIME_RANGE FOREIGN KEY (TIME_RANGE) REFERENCES TIMES_RANGES(ID)
);

CREATE TABLE WAR_PLAYERS (
    ID INT NOT NULL AUTO_INCREMENT,
    WAR INT NOT NULL,
    PLAYER INT NOT NULL,
    WAR_NUMBER INT(2) NOT NULL,
    HAVE_KING TINYINT(1)  NOT NULL DEFAULT 1,
    HAVE_QUEEN TINYINT(1)  NOT NULL DEFAULT 1,
    HAVE_POTIONS TINYINT(1)  NOT NULL DEFAULT 1,
    COMMENT VARCHAR(2500),

    CONSTRAINT PK_PLAYER_IN_WAR PRIMARY KEY (ID),
    CONSTRAINT FK_PLAYER_IN_WAR FOREIGN KEY (WAR) REFERENCES WARS(ID),
    CONSTRAINT FK_PLAYER_IN_WAR_PLAYER FOREIGN KEY (PLAYER) REFERENCES PLAYERS(ID)
);
CREATE TABLE WAR_PLAYERS_ATTACKS (
    WAR_PLAYER INT NOT NULL,
    ATTACK INT NOT NULL,
    TYPE_ATTACK INT NOT NULL,

    CONSTRAINT PK_WAR_PLAYER_ATTACK PRIMARY KEY (WAR_PLAYER, ATTACK),
    CONSTRAINT FK_WAR_PLAYER_ATTACK_WAR_PLAYER
        FOREIGN KEY (WAR_PLAYER) REFERENCES WAR_PLAYERS(ID),
    CONSTRAINT FK_WAR_PLAYER_ATTACK_ATTACK
        FOREIGN KEY (ATTACK) REFERENCES ATTACKS(ID),
    CONSTRAINT FK_WAR_PLAYER_ATTACK_TYPE
        FOREIGN KEY (TYPE_ATTACK) REFERENCES TYPE_ATTACK(ID)

);


我不知道我在做什么错。 (我没有太多时间休眠)

在此先感谢您和最诚挚的问候。

最佳答案

我在使用EclipseLink实现时遇到了类似的问题,但我仅解决了prder中的callink .size()方法来初始化懒惰的集会。见How to load lazy fetched items from Hibernate/JPA in my controller

07-24 22:08