Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JPA, relational databases and etc., What is a Tuple? [closed]

I'm studying Hibernate and JPA, and I am finding this term all the time. Could someone explain me in a practical and didactic way, what is this term, and how it relates to JPA / Hibernate / Databases ?

I wonder if it's a type of data structure that contains any type of data, but I'm not sure if it is really so. Also, I do not know its relation in a query with JPA... can anyone give me some light?

EDIT

Guys, do not precipitate. I have searched on google, in a dictionary (english), in wikipedia, here in the community (stackoverflow), and also the JPA Tuple API.

Here are some of my search terms:

  • What is Tuple
  • Tuple
  • Databases Tuples
  • JPA Tuple

All the documentation of the Java EE 6 says about Tuple is as follows:

Interface for extracting the elements of a query result tuple.

That explains "A LOT".

I can not find something concrete, solid, concise, really explaining what it is. It is difficult for some mortals like me, born not knowing everything. I do not speak English natively, I do not have access to the entire contents of the universe, and sometimes I try to make this community the most practical, simple, friendly, and respectable in questions and answers. It's never enough. Do not presume that I did not research anything before coming here, that I am ignorant, lay, lazy or etc. This is preconception!

For the love of God... I use a translator to write, and I do not have practical understanding about certain things that for others is so easy to understand. My question even follows the rules for questions imposed here on stackoverflow. It's always something when I ask something. When I try to give answers, I always try to research before, and respond as best I can to other users. Give me some remorse sometimes participate in some communities... I ask for knowledge, not for status, and I'm sure others may have the same questions I have. Often people treat this community like a taboo, and not as a place to exchange knowledge. Again... for the love of God...

like image 923
Loa Avatar asked Mar 18 '16 06:03

Loa


Video Answer


2 Answers

Tuple is a generic term taken from mathematics, which just means "several values in specific order". If you can say "give me 2nd value of this thing" then the thing is a tuple.

There is no single notation for them, but you can imagine that (1,2,3), ("a","b","c") and ("John", "Smith", 12, false) are all examples of tuples, having respectively 3, 3 and 4 items. Note that the idea of tuple puts no restrictions on the values, so they can have different types.

Technically a List, String[] and Vector are all tuples. But in languages like Java, the term is used mostly when the items are of different types - otherwise we talk about sequences.

In libraries like JPA the term is used interchangebly with terms record and row. Tuples can be represented by special classes, but one way often used in Java is just to have an Object[].

like image 177
fdreger Avatar answered Oct 20 '22 17:10

fdreger


As was said, a Tuple is an ordered list of things. It helps to understand why you would use such a thing. Consider that you have a table in the database and for whatever reason you only want to select some of the columns from that table. From such a query you can get Objects and Tuples. Example, consider an Entity that has two fields, latitude and longitude:

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

    private BigDecimal latitude;
    private BigDecimal longitude;

When you want to query only the latitude and longitude columns, where do you put the results? One of the ways to do it is to use Tuples:

// With objects, the alias information is lost.
List<Object[]> s = em.createQuery("select u.lattitude as lattitude, u.longitude as longitude from User u", Object[].class).getResultList();
s.stream().forEach((record) -> {
    System.out.println(record[0]+","+record[1]);
});

// With Tuples, the alias information is retained.
List<Tuple> t = em.createQuery("select u.lattitude as lattitude, u.longitude as longitude from User u", Tuple.class).getResultList();
t.stream().forEach((record) -> {
    System.out.println( record.get("lattitude") +","+record.get("longitude"));
});

As you can see, Tuples have an alias property that gets filled out by the query, which you can then use later. This is more convenient than an Object. Also, TupleElement supports Java Generics, so you can also have more type safety than you can have with Objects.

like image 43
K.Nicholas Avatar answered Oct 20 '22 16:10

K.Nicholas