Java Hibernate注释中的映射

Java Hibernate注释中的映射,java,hibernate,annotations,mapping,Java,Hibernate,Annotations,Mapping,我需要知道如何在hibernate中获得一个完整的查询,以获得一个包含完整游戏数据的列表,其中包含类别和其他游戏数据转换 现在,我在游戏中有两个接口属性,但Hibernate不允许映射它们,这是正常的,因为接口不是持久实体,Hibernate无法知道 一些解决方案?。谢谢 我有这个BD结构: 我的休眠设置: 接口 public interface GameInt { int getGame_id(); void setGame_id(int game_id); S

我需要知道如何在hibernate中获得一个完整的查询,以获得一个包含完整游戏数据的列表,其中包含类别和其他游戏数据转换

现在,我在游戏中有两个接口属性,但Hibernate不允许映射它们,这是正常的,因为接口不是持久实体,Hibernate无法知道

一些解决方案?。谢谢

我有这个BD结构:

我的休眠设置:

接口

public interface GameInt {

    int getGame_id();

    void setGame_id(int game_id);

    String getTitle();

    void setTitle(String title);

    String getDescription();

    void setDescription(String description);

}

public interface CategoryInt{

    int getCategory_id();

    void setCategory_id(int category_id);

    String getName();

    void setName(String name);

    String getDescription();

    void setDescription(String description);

    void setCategory(Category category);

    Category getCategory();
}
@MappedSuperclass
public class GameLang implements GameInt {

    private int game_id;
    private String title;
    private String description;

    @Override
    @Id
    public int getGame_id() {
        return game_id;
    }

    @Override
    public void setGame_id(int game_id) {
        this.game_id = game_id;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

}

@MappedSuperclass
public abstract class CategoryLang implements CategoryInt{

    private int category_id;
    private String name;
    private String description;
    private Category category;

    @Override
    @Id
    public int getCategory_id() {
        return category_id;
    }

    @Override
    public void setCategory_id(int category_id) {
        this.category_id = category_id;
    }

    @Override
    @Size(max = 50)
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    @Size(max = 350)
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public Category getCategory() {
        return category;
    }

    @Override
    public void setCategory(Category category) {
        this.category = category;
    }
}
@Entity
@Table(name="es_games")
public class GameES extends GameLang implements Serializable {}

@Entity
@Table(name="en_games")
public class GameEN extends GameLang implements Serializable {}

@Entity
@Table(name = "es_categories")
public class CategoryES extends CategoryLang implements Serializable {}

@Entity
@Table(name = "en_categories")
public class CategoryEN extends CategoryLang implements Serializable {}

@Entity
@Table(name="categories")
public class Category implements Serializable {

    private Integer id;
    private boolean active;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

}

@Entity
@Table(name = "games")
public class Game implements Serializable {

    private int id;
    private Integer categories_id;
    private Date date_start;
    private Date date_expire;
    private boolean active;
    private Integer game_size;
    private String position_one;
    private String position_two;
    private String position_three;
    private CategoryInt category;
    private GameInt game;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotNull
    public Integer getCategories_id() {
        return categories_id;
    }

    public void setCategories_id(Integer categories_id) {
        this.categories_id = categories_id;
    }

    @NotNull
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getDate_start() {
        return date_start;
    }

    public void setDate_start(Date date_start) {
        this.date_start = date_start;
    }

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getDate_expire() {
        return date_expire;
    }

    public void setDate_expire(Date date_expire) {
        this.date_expire = date_expire;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Integer getGame_size() {
        return game_size;
    }

    public void setGame_size(Integer game_size) {
        this.game_size = game_size;
    }

    @Size(min = 4, max = 50)
    public String getPosition_one() {
        return position_one;
    }

    public void setPosition_one(String position_one) {
        this.position_one = position_one;
    }

    @Size(min = 4, max = 50)
    public String getPosition_two() {
        return position_two;
    }

    public void setPosition_two(String position_two) {
        this.position_two = position_two;
    }

    @Size(min = 4, max = 50)
    public String getPosition_three() {
        return position_three;
    }

    public void setPosition_three(String position_three) {
        this.position_three = position_three;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public CategoryInt getCategory() {
        return category;
    }

    public void setCategory(CategoryInt category) {
        this.category = category;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public GameInt getGame() {
        return game;
    }

    public void setGame(GameInt game) {
        this.game = game;
    }
}
持久抽象超类

public interface GameInt {

    int getGame_id();

    void setGame_id(int game_id);

    String getTitle();

    void setTitle(String title);

    String getDescription();

    void setDescription(String description);

}

public interface CategoryInt{

    int getCategory_id();

    void setCategory_id(int category_id);

    String getName();

    void setName(String name);

    String getDescription();

    void setDescription(String description);

    void setCategory(Category category);

    Category getCategory();
}
@MappedSuperclass
public class GameLang implements GameInt {

    private int game_id;
    private String title;
    private String description;

    @Override
    @Id
    public int getGame_id() {
        return game_id;
    }

    @Override
    public void setGame_id(int game_id) {
        this.game_id = game_id;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

}

@MappedSuperclass
public abstract class CategoryLang implements CategoryInt{

    private int category_id;
    private String name;
    private String description;
    private Category category;

    @Override
    @Id
    public int getCategory_id() {
        return category_id;
    }

    @Override
    public void setCategory_id(int category_id) {
        this.category_id = category_id;
    }

    @Override
    @Size(max = 50)
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    @Size(max = 350)
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public Category getCategory() {
        return category;
    }

    @Override
    public void setCategory(Category category) {
        this.category = category;
    }
}
@Entity
@Table(name="es_games")
public class GameES extends GameLang implements Serializable {}

@Entity
@Table(name="en_games")
public class GameEN extends GameLang implements Serializable {}

@Entity
@Table(name = "es_categories")
public class CategoryES extends CategoryLang implements Serializable {}

@Entity
@Table(name = "en_categories")
public class CategoryEN extends CategoryLang implements Serializable {}

@Entity
@Table(name="categories")
public class Category implements Serializable {

    private Integer id;
    private boolean active;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

}

@Entity
@Table(name = "games")
public class Game implements Serializable {

    private int id;
    private Integer categories_id;
    private Date date_start;
    private Date date_expire;
    private boolean active;
    private Integer game_size;
    private String position_one;
    private String position_two;
    private String position_three;
    private CategoryInt category;
    private GameInt game;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotNull
    public Integer getCategories_id() {
        return categories_id;
    }

    public void setCategories_id(Integer categories_id) {
        this.categories_id = categories_id;
    }

    @NotNull
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getDate_start() {
        return date_start;
    }

    public void setDate_start(Date date_start) {
        this.date_start = date_start;
    }

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getDate_expire() {
        return date_expire;
    }

    public void setDate_expire(Date date_expire) {
        this.date_expire = date_expire;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Integer getGame_size() {
        return game_size;
    }

    public void setGame_size(Integer game_size) {
        this.game_size = game_size;
    }

    @Size(min = 4, max = 50)
    public String getPosition_one() {
        return position_one;
    }

    public void setPosition_one(String position_one) {
        this.position_one = position_one;
    }

    @Size(min = 4, max = 50)
    public String getPosition_two() {
        return position_two;
    }

    public void setPosition_two(String position_two) {
        this.position_two = position_two;
    }

    @Size(min = 4, max = 50)
    public String getPosition_three() {
        return position_three;
    }

    public void setPosition_three(String position_three) {
        this.position_three = position_three;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public CategoryInt getCategory() {
        return category;
    }

    public void setCategory(CategoryInt category) {
        this.category = category;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public GameInt getGame() {
        return game;
    }

    public void setGame(GameInt game) {
        this.game = game;
    }
}
和持续的期末课程

public interface GameInt {

    int getGame_id();

    void setGame_id(int game_id);

    String getTitle();

    void setTitle(String title);

    String getDescription();

    void setDescription(String description);

}

public interface CategoryInt{

    int getCategory_id();

    void setCategory_id(int category_id);

    String getName();

    void setName(String name);

    String getDescription();

    void setDescription(String description);

    void setCategory(Category category);

    Category getCategory();
}
@MappedSuperclass
public class GameLang implements GameInt {

    private int game_id;
    private String title;
    private String description;

    @Override
    @Id
    public int getGame_id() {
        return game_id;
    }

    @Override
    public void setGame_id(int game_id) {
        this.game_id = game_id;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

}

@MappedSuperclass
public abstract class CategoryLang implements CategoryInt{

    private int category_id;
    private String name;
    private String description;
    private Category category;

    @Override
    @Id
    public int getCategory_id() {
        return category_id;
    }

    @Override
    public void setCategory_id(int category_id) {
        this.category_id = category_id;
    }

    @Override
    @Size(max = 50)
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    @Size(max = 350)
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public Category getCategory() {
        return category;
    }

    @Override
    public void setCategory(Category category) {
        this.category = category;
    }
}
@Entity
@Table(name="es_games")
public class GameES extends GameLang implements Serializable {}

@Entity
@Table(name="en_games")
public class GameEN extends GameLang implements Serializable {}

@Entity
@Table(name = "es_categories")
public class CategoryES extends CategoryLang implements Serializable {}

@Entity
@Table(name = "en_categories")
public class CategoryEN extends CategoryLang implements Serializable {}

@Entity
@Table(name="categories")
public class Category implements Serializable {

    private Integer id;
    private boolean active;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

}

@Entity
@Table(name = "games")
public class Game implements Serializable {

    private int id;
    private Integer categories_id;
    private Date date_start;
    private Date date_expire;
    private boolean active;
    private Integer game_size;
    private String position_one;
    private String position_two;
    private String position_three;
    private CategoryInt category;
    private GameInt game;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotNull
    public Integer getCategories_id() {
        return categories_id;
    }

    public void setCategories_id(Integer categories_id) {
        this.categories_id = categories_id;
    }

    @NotNull
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getDate_start() {
        return date_start;
    }

    public void setDate_start(Date date_start) {
        this.date_start = date_start;
    }

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getDate_expire() {
        return date_expire;
    }

    public void setDate_expire(Date date_expire) {
        this.date_expire = date_expire;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Integer getGame_size() {
        return game_size;
    }

    public void setGame_size(Integer game_size) {
        this.game_size = game_size;
    }

    @Size(min = 4, max = 50)
    public String getPosition_one() {
        return position_one;
    }

    public void setPosition_one(String position_one) {
        this.position_one = position_one;
    }

    @Size(min = 4, max = 50)
    public String getPosition_two() {
        return position_two;
    }

    public void setPosition_two(String position_two) {
        this.position_two = position_two;
    }

    @Size(min = 4, max = 50)
    public String getPosition_three() {
        return position_three;
    }

    public void setPosition_three(String position_three) {
        this.position_three = position_three;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public CategoryInt getCategory() {
        return category;
    }

    public void setCategory(CategoryInt category) {
        this.category = category;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public GameInt getGame() {
        return game;
    }

    public void setGame(GameInt game) {
        this.game = game;
    }
}
最终结论。

我的意思是这是不可能做到的,解决这个问题需要使用HQL

在本例中,我构建了这个POJO

public class GameCategoryByLang {

    private Game game;
    private GameInt gameLang;
    private CategoryInt categoryLang;

    public GameCategoryByLang(Game game, GameInt gameLang, CategoryInt categoryLang) {
        this.game = game;
        this.gameLang = gameLang;
        this.categoryLang = categoryLang;
    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public GameInt getGameLang() {
        return gameLang;
    }

    public void setGameLang(GameInt gameLang) {
        this.gameLang = gameLang;
    }

    public CategoryInt getCategoryLang() {
        return categoryLang;
    }

    public void setCategoryLang(CategoryInt categoryLang) {
        this.categoryLang = categoryLang;
    }
}
并使用此HQL使用数据实现它

String hql = "select new my.package.GameCategoryByLang(g, gx, c) from Game g, Game" + lang.toUpperCase() + " gx, Category" + lang.toUpperCase() + " c WHERE g.id = gx.game_id and g.id = c.category_id";

嗯,数据库模式很糟糕-当你扩展到俄罗斯、中国、印度时,你打算怎么做??您应该有一个表用于分类,并有一个列用于区分国家,对于游戏也是如此。然后查询就会容易得多。

下一步最好做:
es_games
en_games
在一张表上更改-
所有_games
带有字段
lang

接下来,您可以执行一个超类

@javax.persistence.Entity
@javax.persistence.Inheritance
@javax.persistence.Table(name = "all_games")
@javax.persistence.DiscriminatorColumn(name = "LANG")
@javax.persistence.DiscriminatorValue("nknown")
@org.hibernate.annotations.DiscriminatorFormula("case when LANG in "
+ "('es', 'en') "
+ "then LANG else 'unknown' end")
public class AllGames implements java.io.Serializable{  

// ..  all general getters and setters

}  
并生成子类

@javax.persistence.Entity
@javax.persistence.DiscriminatorValue("en")
public class GameEN extends AllGames implements java.io.Serializable
{  
// ..  
}  
@javax.persistence.Entity
@javax.persistence.DiscriminatorValue("es")
public class GameES extends AllGames implements java.io.Serializable
{  
// ..  
}   

您可以对
类别执行相同的操作

是的,但对我来说不是很好的解决方案,因为并非所有类别都是或所有游戏,或所有游戏查询都必须国际化,每个类别或游戏。。。将以某些语言进行国际化。这个解决方案对我来说更好,每个国际化的db表只有必要的国际化数据。所有的应用程序都是通过界面开发的,并且是完全动态的。嗨,朋友,这对其他情况来说是强大的(我已经学会了),但是在这种情况下,并不是所有的游戏都完全国际化,所以我想这不是很好,因为游戏表会增加得更快。我非常感谢您的关注。我知道这是一个离题的话题,但我可以问一下您使用的UML工具的名称吗?不是任何特殊的UML工具,图像是用MySQL Workbench UML工具制作的。