Java Hibernate. Search for the key

-1

Greetings to the whole community. I have a problem to perform a search on a table. The key of this table are two fields and I am doing the search with hibernate: The model is three tables. REGION, CITY, COMMUNITY. For the CITY table, the key consists of two fields: codigoregion and codigocity. However for the search I have a mess: session.get (City.class, pcodigociudad). Question: How do I associate the pcodigoregion ?. I can not use annotations, nor can I use session.createquery. I need it to be with session.get ( Your help will be greatly appreciated. Larry

    
asked by LARRY30001 08.11.2017 в 22:52
source

1 answer

0

This is a table with a composite primary key. Therefore, you must have both keys as attributes, and be headed by the @Id annotation.

For example:

@Id
private Integer codigoregion;

@Id
private Integer codigociudad;

Also, since both are key, you must add a constructor that receives both parameters.

public Ciudad(Integer region, Integer ciudad) {
         codigoregion = region;
         codigociudad = ciudad;
}

And with this, you should be able to search with Hibernate. In the parameter where you pass the key you are looking for, you must pass a City object with the keys you are looking for.

Ciudad pcodigociudad = new Ciudad(1, 2);
sesion.get(Ciudad.class, pcodigociudad);

(It would return the city that has codigoregion 1 and codigocity 2)

    
answered by 20.11.2017 в 21:14