How to create a multi-position display in sleep mode?

I want to create many-one mapping between two tables, Expense (ID, NAME, CATEGORY) and Category (ID, NAME). In my class, I created the Category Category field and its setters and getters. I made them by seeing some things from the Internet. What changes should I make in my Category.java class. So far it looks

public class Category{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;

public Category() {
}

public int getCatId() {
    return this.catId;
}

public void setCatId(int catId) {
    this.catId = catId;
}

public String getCatName() {
    return this.catName;
}

public void setCatName(String catName) {
    this.catName = catName;
}

}

I do not want to make comparisons with xml config. I think annotations are good for a newbie like me.

And my old one! The SQL query looks like this:

SELECT EXPENSES.EXPNS_ID, EXPENSES.CAT_ID, EXPENSES.NAME, CATEGORY.CAT_NAME FROM EXPENSES INNER JOIN CATEGORY ON EXPENSES.CAT_ID = CATEGORY.CAT_ID WHERE USER_NAME="+currentUserName

How to use internal join in hibernate?

Any suggestions!!

Thank!


Update

Thanks to all the defendants, I tried what you said and returns an empty list.

, 'userName = Tamil', . , Hibernate, ,

select expens0_.expnsId as expnsId1_, expens0_.catId as catId1_, expens0_.category_catId as category7_1_, expens0_.userName as userName1_ from Expens expens0_ inner join Category category1_ on expens0_.category_catId=category1_.catId where expens0_.userName=?

, JPQL, catName Category[catId, catName] . catId Expens[expnsId, catId, userName].

Expens.java, catName Expens.

@ManyToOne
private Category category

// getters, setters

. , , . , , .

pascal: Query query = hSession.createQuery("SELECT e FROM Expens e JOIN e.category c WHERE e.userName = :userName").setParameter("userName", userName);

, , , SQl. .

+3
3

, , , Hibernate Annotations Reference (, ). .

, :

@Entity
public class Expense {
    @Id @GeneratedValue
    private Long;

    @ManyToOne
    private Category category

    // getters, setters
    ...
}

.

, OneToMany ( mappedBy, ):

@Entity
public class Category {
    @Id @GeneratedValue
    private Long id;

    @OneToMany(mappedBy="category")
    private Set<Expense> expenses = new HashSet<Expense>();
    ....
}

JPQL :

SELECT e FROM Expense e JOIN e.category c WHERE e.username = :username  

: JDBC . Hibernate , HQL ( ) List<Expense>. , . :

List<Expense> expenses = ... // some code to retrieve a list by username

for (Expense expense : expenses) {
    System.out.println(expense.getCategory().getName());
}

+4

Expense :

@ManyToOne
@JoinColumn(name="CATEGORY_ID")
private Category category

, , " ", :

@OneToMany
private List<Expense> expenses;

, , @OneToMany - / , - .. HQL ( ), .

+2

As Bojo suggested

@ManyToOne(fetch=FetchType.EAGER) // Gonna be eager by default anyway
@JoinColumn(name="CATEGORY_ID")
private Category category;

Plus it's in your class category to make it bidirectional,

@OneToMany(mappedBy="category")
private List<Expense> expense;

You do not need to do such an inner join. When you request an expense, the corresponding category is automatically downloaded with impatience, most likely through a connection.

+2
source

Source: https://habr.com/ru/post/1765946/


All Articles