How to retrieve value of Releation Table's column - java

i am new to hibernate. currently i am stuck in one problem.
i have no idea how to retrieve the value of relation table column.
senario:
i have three tables.
TableA:
-------
a_id
a_col1
TableB:
-------
b_id
b_col1
TableC
-------
c_id
a_id
b_id
c_col1
i have created model class for TableA and TableB. i have mapped them using #ManyToMany and #JoinTable annotation
TableA
{
...
private Set<TableB> tableB;
#ManyToMany
#JoinTable( name="TableC",
joinColumns={#JoinColumn(name="a_id")},
inverseJoinColumns={#JoinColumn(name="b_id)}
public getTableB()
{
return tableB;
}
public setTableB(...) { ... }
}
now my question is:
===================
how can i retrieve the value of c_col1 from TableA entity

You can't. What you're seeing is that you don't actually have a many-to-many relationship between A and B. You have three objects: A, B, and C, with a one-to-many from C to A and from C to B. Then your c_col1 column will exist on C.

Related

Hibernate relation between a sub table and related table

Have a relation between a sub table and a related table using the primary table. Any thoughts about how to go about the hibernate relation.
A <---> B (One to One relation) B is a sub-table of A.
B <---> C (B has One to Many Relation with C Using A)
It not needed in table B in your case. There is #OneToMany between A and B.
#Entity
#Table(name="A")
public class A {
....
#OneToMany(mappedBy="a")
private B b;
}
#Entity
#Table(name="C")
public class C {
...
#OneToOne
#JoinColume(name="ref_to_a")
private A a;
}
Additional table B is required if #ManyToMany association take place.

PostgreSQL inheritance + Hibernate InheritanceType.TABLE_PER_CLASS causing more than one row exception

Parent table and class
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
#Table(name = "vehicle")
public class Vehicle extends Entity {
// Attributes
}
create table vehicle
(
id varchar2(50) not null,
// Attributes
)tablespace :TABLESPACE_DATA ;
Child 1 table and class
#Entity
#Table(name = "car")
public class Car extends Vehicle {
// Attributes
}
create table car
(
// Attributes
) inherits (vehicle) tablespace :TABLESPACE_DATA ;
Child 2 table and class
#Entity
#Table(name = "bus")
public class Bus extends Vehicle {
// Attributes
}
create table bus
(
// Attributes
) inherits (vehicle) tablespace :TABLESPACE_DATA ;
If I add a new Car, then there will be two records with same id, one in a parent table, other in a child table.
Then with a Hibernate generated query (check below) it will return with two rows, which cause an exception.
select
// ...
from
(
select
id,
//
0 as clazz_
from
VEHICLE
union all
select
id,
//
1 as clazz_
from
car
union all
select
id,
//
2 as clazz_
from
bus
) vehicle0_
where
vehicle0_.id = '5105f2d9-9c69-44c1-9368-0b3013a3a058'
The Hibernate version 5.4.28.
What could be the issue?
The problem was the missing "abstract" keyword. After I added it to the Vehicle then the Hibernate skipped the parent from the query.
Solution found here: Hibernate TABLE_PER_CLASS with #MappedSuperclass won't create UNION query
That should not happen. When doing an insert, Hibernate ORM should only insert that entity in the right table. So if you create a new Car, only an insert in the car table should occur.
Hibernate ORM expects this mapping to work with three separates table. The reason it does an union is because if you run a query like "from Vehicle", it needs to check all the tables.
Check the Hibernate ORM documentation for more details about the mapping.

JPA 2.1 / Hibernate - Joining 2 unrelated entities into the first one entity that has #OneToMany relation to third entity

I am sorry if the Subject was misleading but here is the scenario that I have.
#Entity
Class A {
#Id
private String id;
#OneToMany
private Set<B> b;
}
#Entity
Class B {
#Id
private String c;
private String d;
}
#Entity
Class C {
#Id
private String e;
private String f;
}
Currently, I am retrieving the entity A with its set of objects from Entity B. The thing is that Entity B and Entity C are not related by foreign key and they need to be joined by their primary keys, so when I retrieve the entity A, in the set of objects, I will get the columns from the entities B and C joined together.
Is there any way to tell Hibernate to join the columns from entities B and C when I try to get the object A ?
In JPA 2.0 for joining related entities join is used. For this FK was needed.
Problem with joining unrelated entities is solved in JPA 2.1 standard, Hibernate 5.1 and above. You can use join on unrelated columns.

ManyToMany Relation in JPA with Index possible?

I want to have a many to many relation between to Entities and I'm using a junction table for it right now on the MySql end. Now I need a JPA solution like an index which allows me to use the id's of both of those tables/ entities as a Key/ Index without the Entities itself to avoid some cross package references. When querying I have the ID of EntityA and want to find the ID of EntityB with it, nothing more. This is how I thought it might work:
(It doesn't because I don't have IDs for the JunctionEntity and if I use IDs then obviously the entries have to be unique when the only unique Thing should be both entries together. A PK Class isn't working either since it would still require said references to both entities)
EntityA:
#Entity
#Table(name = "EntityA")
})
public class EntityA {
#Id
private int id;
}
EntityB:
#Entity
#Table(name = "EntityB")
})
public class EntityB {
#Id
private int id;
}
JunctionEntity:
#Entity
#Table(name = "junction", indexes = {
#Index(name = "ix_a_b", columnList = "a_id, b_id")
})
public class JunctionEntity {
private int a_id;
private int b_id;
}
MySQL for JunctionTable:
CREATE TABLE junction (
a_id INT NOT NULL,
b_id INT NOT NULL,
CONSTRAINT junction_fk_a FOREIGN KEY (a_id) REFERENCES entityA (id),
CONSTRAINT junction_fk_b FOREIGN KEY (b_id) REFERENCES entityB (id)
);
CREATE UNIQUE INDEX ix_a_b
ON junction (a_id, b_id);
You can add a primary key and search in junction table using a_id, b_id or both.
CREATE TABLE junction (
id INT NOT NULL,
a_id INT NOT NULL,
b_id INT NOT NULL,
CONSTRAINT junction_fk_a FOREIGN KEY (a_id) REFERENCES entityA (id),
CONSTRAINT junction_fk_b FOREIGN KEY (b_id) REFERENCES entityB (id)
);
You don't need to know the id of junction table. You can query the junction like this:
select b_id from junction where a_id = ?;

How Can I write The Following Query In hibernate Criteria

Select * from Table A left join Table B on A.id=B.id and A.langCode='IN';
where 'IN' is input from user.
Table A and Table B has Mapping For Id but there is no Mapping with LangCode between the two as table B dosent have an column called langCode to map with , i want to write the following query using hibernate criteria without mapping langcode.
Table: Employee :
EMP_ID - primary key ,
NAME ,
CONTACT_DETAILS
Table:Employee_Lang:
EMP_ID- composite primary key,
LANG_CODE- composite primary key,
NAME
Actual Query:
Select * from Employee Emp left outer join Employee_Lang EmpLang on Emp.EMP_ID=EmpLang.EMP_ID AND EmpLang.LANG_CODE='IN'
I have mapped only the Emp_Id as primary key from both the tables in hibernate hence hibernate criteria will only apply a join on that
And not on LangCode.
Note:-I cant change hibernate mapping and can use only hibernate Criteria , as per the clients requirement, please help me on this one.
For example you have two models:
#Entity
class Employee {
#Id
private Long id;
#OneToOne
private EmployeeLang employeeLang;
...
}
#Entity
class EmployeeLang {
#Id
private Long id;
private String langCode;
#OneToOne
private Employee employee;
...
}
You should have some mapping between them and then you can do next criteria:
Criteria criteria = session.createCriteria(Employee.class);
criteria.createAlias("employeeLang", "empLang"); // inner join with EmployeeLang
criteria.add(Restrictions.eq("empLang.langCode", "in");
List<Employee> employees = criteria.list();

Categories

Resources