gpt4 book ai didi

java - Hibernate 注释中的映射

转载 作者:行者123 更新时间:2023-11-30 09:40:09 26 4
gpt4 key购买 nike

我需要知道如何在 Hibernate 中获得一个完整的查询,以获取一个列表,其中包含具有类别和其他游戏数据翻译的完整数据的游戏。

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

一些解决方案?谢谢!

我有这个 BD 结构:

My EER

我的 hibernate 设置:

接口(interface)

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();
}

持久抽象父类(super class)

@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;
}
}

和持久的 final类

@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";

最佳答案

好吧,数据库模式很糟糕——当你扩展到俄罗斯、中国、印度时你打算做什么??你应该有一个类别表和一列来区分国家,游戏也是如此。那么查询就会容易很多。

关于java - Hibernate 注释中的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9546400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com