0

I have two classes:

@Entity
public class Tick {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@ManyToOne(optional = false)
@JoinColumn(name = "elitesystem_id", referencedColumnName = "id")
private EliteSystem eliteSystem;

private Date createDate;

@ManyToOne(optional = true)
@JoinColumn(name = "commander_id", referencedColumnName = "id")
private Commander commander;

private String address;

and

@Entity
public class Note {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@ManyToOne(optional = true)
@JoinColumn(name = "tick_id", referencedColumnName = "id")
private Tick tick;

private String text;

private Date createDate;

I want to select all ticks and get notes if there are any:

    Query query = session.createQuery("select t, n from Note n right join n.tick t where t.commander.name = '123'");
    List<Object[]> list = query.list();

This returns only Tick objects. What is the correct approach to get the Note information as well in 1 single query? I could put a reference to a Note into the Tick class, but this doesnt sound right, as there are only a few notes, so the column in the Tick table would mostly be empty.

1 Answer 1

1

Create a New class for example:

public class TickNote {
    private Tick tick;
    private Note note;

    public TickNote(Tick tick,Note note){
        this.tick=tick;
        this.note=note;

Then your query is:

Query query = session.createQuery("select NEW TickNote(t, n) from Note n right join n.tick t where t.commander.name = '123'");
    List<TickNote> list = query.list();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.