relaciones relacionar relacional relacion namedquery muchos mapeo manytomany entidades ejemplos ejemplo con completo java hibernate jpa orm hibernate-mapping

java - relacionar - Cómo unir tablas en clave no principal usando JPA e Hibernate



relacion manytomany jpa (1)

Así es como debe verse su mapeo:

@Entity public class User { @Id private Long id; private String name; @OneToMany(mappedBy = "user") private List<UserHouseMap> houses = new ArrayList<>(); } @Entity public class House { @Id @Column(name = "house_name", nullable = false, unique = true) private String house_name; private String address; @OneToMany(mappedBy = "house") private List<UserHouseMap> users = new ArrayList<>(); } @Entity public class UserHouseMap implements Serializable { @Id @ManyToOne @JoinColumn(name = "user_name", referencedColumnName = "name") private User user; @Id @ManyToOne @JoinColumn(name = "house_name", referencedColumnName = "house_name") private House house; }

Tanto el User como la House tienen acceso a sus entidades UserHouseMap asociadas, que coinciden con el esquema de la base de datos.

Usar dos asociaciones de uno a muchos siempre es mejor que confiar en relaciones de muchos a muchos.

Tengo 3 modelos User , House , UserHouseMap . Y necesito acceder a la casa de un usuario a través del mapa. El único problema es que este es un DB anterior y no puedo cambiar el hecho de que necesito asignar User a UserHouseMap usando user.name , que es una clave no primaria.

Hibernate sigue dándome errores diciendo que necesito tenerlo como la clave principal o recibo errores que dicen A JPA error occurred (Unable to build EntityManagerFactory): Unable to find column with logical name: name in org.hibernate.mapping.Table(users) and its related supertables and secondary tables

He intentado con @Formula como una solución, pero eso no funcionó. También probé @JoinColumnOrFormula pero tampoco funcionó. Aquí está mi solución con @Formula

@Expose @ManyToOne(targetEntity = House.class) @Formula("(select * from houses inner join user_house_map on houses.house_name = user_house_map.house_name where user_house_map.user_name=name)") public House house;

Aquí estaba mi intento de una solución @JoinColumnOrFormula .

@Expose @ManyToOne(targetEntity = House.class) @JoinColumnsOrFormulas({ @JoinColumnOrFormula(formula=@JoinFormula(value="select name from users where users.id= id", referencedColumnName="name")), @JoinColumnOrFormula(column = @JoinColumn(name= "house_name", referencedColumnName="house_name")) }) public House house;

Aquí está mi mapeo

@Id @GeneratedValue @Expose public Long id; @Expose @Required @ManyToOne @JoinTable( name="user_house_map", joinColumns= @JoinColumn(unique=true,name="user_name", referencedColumnName="name"), inverseJoinColumns= @JoinColumn(name="house_name", referencedColumnName="house_name")) private House house;

Aquí están los esquemas DB

Usuarios

Table "public.users" Column | Type | Modifiers -----------------------+-----------------------------+----------------------------- name | character varying(255) | id | integer | not null Indexes: "user_pkey" PRIMARY KEY, btree (id) Foreign-key constraints: "housing_fkey" FOREIGN KEY (name) REFERENCES user_house_map(user_name) DEFERRABLE INITIALLY DEFERRED

Casas

Table "public.houses" Column | Type | Modifiers ---------------+------------------------+----------- house_name | character varying(255) | not null address | text | city | text | state | text | zip | integer | zip_ext | integer | phone | text | Indexes: "house_pkey" PRIMARY KEY, btree (house_name) Referenced by: TABLE "user_house_map" CONSTRAINT "house_map_fkey" FOREIGN KEY (house_name) REFERENCES house(house_name) DEFERRABLE INITIALLY DEFERRED

UserHouseMap

Table "public.user_house_map" Column | Type | Modifiers -------------+------------------------+----------- user_name | character varying(255) | not null house_name | character varying(255) | not null Indexes: "user_house_map_pkey" PRIMARY KEY, btree (user_name) "user_house_map_house_key" btree (house_name) Foreign-key constraints: "user_house_map_house_fkey" FOREIGN KEY (house_name) REFERENCES houses(house_name) DEFERRABLE INITIALLY DEFERRED Referenced by: TABLE "users" CONSTRAINT "housing_fkey" FOREIGN KEY (name) REFERENCES user_house_map(user_name) DEFERRABLE INITIALLY DEFERRED