Hibernate查询不返回正确的结果

时间:2015-01-14 13:56:05

标签: java mysql hibernate

我正在开发一个带有休眠和弹簧的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)

);

我不知道自己做错了什么。 (我没有太多时间使用hibernate)

提前致谢并致以最诚挚的问候。

1 个答案:

答案 0 :(得分:0)

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