How do I get Auto Increment on my Id variable? - java

#Entity
public class Component {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;
I only get null on component.id

Related

How to get value from list<class> in java?

I want to get value from arraylist in java.
I can show traininglist in HTML by using
model.addAttribute("traininglist",traininglist);
and get the each value by using :
tr : ${traininglist}
then
tr.trainingduration
but I now I want to get traininglist value in java, please help me here's my code
List<TrainingModel> traininglist = viewTraining(biodataId);
and there's my trainingmodel class
public class TrainingModel {
#Id
#Column(name="id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name="is_delete")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private boolean isDelete;
#Column(name="biodata_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long biodataId;
#Column(name="training_name")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private String trainingName;
#Column(name="organizer")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private String organizer;
#Column(name="training_year")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private String trainingYear;
#Column(name="training_month")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private String trainingMonth;
#Column(name="training_duration")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int trainingDuration;
//getter setter
}
To see all the objects in the arrayList, you can use: System.out.println(traininglist);
or you can loop through the training list like this:
for(TrainingModel listElement : traininglist){
listElement.(use the getter of any of the fields to get the Object property)
}
Hope this helps.

Spring Data Jpa Data fetch where parent entity has foreign key on other entity

i have two entity test1 and test2
#Entity
#Table(name="test1")
class test1{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id",unique = true, nullable = false)
private int Id;
#OneToOne()
#JoinColumn("sub_category_id")
Test2 test2Bean;
//Getter&Setters
}
#Entity
#Table(name="test2")
class test2{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "sub_category_id",unique = true, nullable = false)
private int Id;
#Column(name="name")
private String name;
//Getter&Setters
}
What i need is to get name from test2 entity having sub_category_id
from test1 entity
I tried mapping using JoinCloumn as you can see but failed as give error
of foreign constraint and i don't want to change by table structure
Please Help Thanks in Advance!!

#GeneratedValue - how do I replace it with my function? (Java 8)

Now the generator goes sequentially with step 1.
I need the ID to generate its own function.
How to substitute its function?
Thank you.
#Entity
#Table(name = "MovementHistory")
public class MovementHistory {
#Id
#SequenceGenerator(name="SEQ_GEN_MH", sequenceName="SEQ_MH", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_GEN_MH")
private long id;
private long waybillID;
private Date dateEvent;
#ManyToOne(fetch = FetchType.EAGER)
private Status status;
}

Hibernate/JPA incorrect join in Spring Boot Application

See code below for my 2 entity classes - when I call the findAll() method from my OrigRepository class, it joins these two tables using both primary keys. I want the join to be between the primary key of the Orig table and the foreign key entry in the MsgResponse table ("OrigID") - any sugggestions?
Orig Entity
#Entity
#Table(name = "originator")
public class Orig {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "OrigID")
private int OrigID;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "OrigID")
private MsgResponse responseInfo;
}
MsgResponse Entity
#Entity
#Table(name = "message_response")
public class MsgResponse {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private int responseId;
#Column(name = "OrigID")
private int OrigId;
#OneToOne(mappedBy="responseInfo")
private Orig OrigInfo;
}
I suggest you to see the jpa documentation
here.
Example 1 should be your case
Try to swap relation ownership, that is:
#Entity
#Table(name = "originator")
public class Orig {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "OrigID")
private int OrigID;
#OneToOne(mappedBy="origInfo")
private MsgResponse responseInfo;
}
#Entity
#Table(name = "message_response")
public class MsgResponse {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private int responseId;
// #Column(name = "OrigID")
// private int origId;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "OrigID")
private Orig origInfo;
}
Note that the #JoinColum annotation is in now in the MsgResponse entity. This is because in a #OneToOne the join column refers to the source entity (see here).
Hope this could help.

why is Hibernate selecting same columns twice?

Hibernate:
/* load entities.Department */ select
department0_.name as name4_0_,
department0_.id as id4_0_
from
J_DEPT department0_
where
department0_.name=?
Hibernate:
/* load one-to-many entities.Department.employees */ select
employees0_.dept as dept4_1_,
employees0_.id as id1_,
employees0_.id as id5_0_,
employees0_.dept as dept5_0_,
employees0_.name as name5_0_
from
J_EMP employees0_
where
employees0_.dept=?
Note that ID and DEPT columns are selected twice.
#Entity
#Table(name = "J_EMP")
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
#SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;
private String name;
#ManyToOne
#JoinColumn(name = "dept")
private Department deptNameInEmp;
}
#Entity
#Table(name = "J_DEPT")
public class Department {
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
#SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;
#Id
#Column(name = "name")
private String deptNameInDeptEntity;
#OneToMany(mappedBy = "deptNameInEmp")
Set<Employee> employees;
}
UPDATED:
It was done intentionally to put #GeneratedValue on non-PK. However, now I've updated as you specified.
I've copy-pasted the new queries:
Hibernate:
/* load entities.Department */ select
department0_.id as id4_0_,
department0_.name as name4_0_
from
J_DEPT department0_
where
department0_.id=?
Hibernate:
/* load one-to-many entities.Department.employees */ select
employees0_.dept as dept4_1_,
employees0_.id as id1_,
employees0_.id as id5_0_,
employees0_.dept as dept5_0_,
employees0_.name as name5_0_
from
J_EMP employees0_
where
employees0_.dept=?
I guess its still the same.
And here are the tables:
CREATE TABLE "XYZ"."J_DEPT"
( "ID" NUMBER(*,0) NOT NULL ENABLE,
"NAME" VARCHAR2(255 BYTE) NOT NULL ENABLE,
CONSTRAINT "J_DEPT_PK" PRIMARY KEY ("ID")
)
CREATE TABLE "XYZ"."J_EMP"
( "ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(255 BYTE),
"DEPT" NUMBER NOT NULL ENABLE,
CONSTRAINT "J_EMP_PK" PRIMARY KEY ("ID"))
here is the code -i'm pasting here as it is :
#Entity
#Table(name = "J_DEPT")
public class Department {
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
#SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
#Id
private Long id;
#Column(name = "name")
private String deptNameInDeptEntity;
#OneToMany(mappedBy = "deptNameInEmp")
Set<Employee> employees;
public Set<Employee> getEmployees() {
return employees;
}
}
#Entity
#Table(name = "J_EMP")
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
#SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;
private String name;
#ManyToOne
#JoinColumn(name = "dept")
private Department deptNameInEmp;
public Employee() {
}
And here is the test case:
#RunWith(SpringJUnit4ClassRunner.class)
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
#ContextConfiguration(locations = { "classpath:test-applicationContext.xml" })
#Transactional
public class EmpTest {
#Autowired
private SessionFactory sessionFactory;
#Test
public void testDept() {
final Department find = (Department) sessionFactory.getCurrentSession()
.get(Department.class, Long.parseLong("1"));
System.out.println("find res = " + find);
}
}
Probably because of this part:
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
#SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;
#Id
#Column(name = "name")
private String deptNameInDeptEntity;
There is something wrong here, the GeneratedValue cannot be applied to a non PK. Did you mean:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
#SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;
#Column(name = "name")
private String deptNameInDeptEntity;
If not, and if this was intentional, please explain your goal and show your table(s).
Update: I can't reproduce the problem. I copy pasted the code you provided and here is the query I get:
select
employee37x0_.id as id135_,
employee37x0_.dept as dept135_,
employee37x0_.name as name135_
from
J_EMP employee37x0_
where
employee37x0_.id=?
Works as expected.
A Google search on "hibernate duplicate aliases" reveals some (old) issues so I don't exclude anything but I couldn't find any proof of recent existing problems. Can you provide a test case (using an embedded database)?

Categories