Dani
Dani

Reputation: 4111

Mapping in Hibernate Annotations

I need to know how I can get one complete query in hibernate to get one list with games with full data with category and other game data translations.

Now, I have in Game two interfaces properties but Hibernate not permit mapped them, is normal, because interfaces are not persistent entities and Hibernate cant know it.

Some solution?. Thanks!

I have this BD structure:

My EER

My Hibernate Setup:

Interfaces

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

Persistent abstract superclasses

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

And persistent final classes

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

FINAL CONCLUSION.

How I mean this is imposible to do, to resolve question is necesary use HQL.

In this case, I built this 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;
    }
}

And implement it with data with this 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";

Upvotes: 2

Views: 535

Answers (2)

Ilya
Ilya

Reputation: 29673

Is better to do next: es_games, en_games change on one table - all_games with field lang

next you can do one superclass

@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

}  

and make subclasses

@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
{  
// ..  
}   

The same you can do with categories

Upvotes: 1

František Hartman
František Hartman

Reputation: 15076

Well the database schema is terrible - what are you going to do when you expand to russia, china, india?? you should have one table for category and have a column to distinguish between countries, same for games. Then the queries will be much easier.

Upvotes: 1

Related Questions