How to get value from list<class> in java? - 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.

Related

How do I get Auto Increment on my Id variable?

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

hql: cast string to long

when I try to cast a string in my repository I got invalid request
how can I cast entity2_ID to long without change it to an object inside Entity1?
public class Entity1{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "Entity2_ID")
private String entity2_ID;
}
public class Entity2{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID", nullable = false)
private Long id;
}
public interface Entity1Repository extend JpaRepository<Entity1, Long>{
#Query("select e1 from Entity1 e1 where cast(e1.entity2_ID as long) in (:entities)")
List<Entity1> findAllById(#Param("entities") List<Entity2> entities);
}

JSON : There is a cycle in the hierarchy

public class OrderEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="order_id")
private int orderid;
#ManyToOne
#JoinColumn(name="user_id", nullable=false)
private UserEntity user;
#OneToMany(mappedBy="ordersBid",fetch=FetchType.EAGER)
private Set<BidPriceEntity> bidOrders;
}
public class BidPriceEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#ManyToOne
#JoinColumn(name="order_Id",nullable=false)
private OrderEntity ordersBid;
#ManyToOne
#JoinColumn(name="driver_Id",nullable=false)
private UserEntity driver;
#Column(name="bid_price")
private double bidPrice;
}
public class OrderEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="order_id")
private int orderid;
#ManyToOne
#JoinColumn(name="user_id", nullable=false)
private UserEntity user;
#OneToMany(mappedBy="ordersBid",fetch=FetchType.EAGER)
private Set<BidPriceEntity> bidOrders;
}
Here are the three entities.I am trying to tansfer the data(retrieved from database) to JsonArray.it always get error :There is a cycle in the hierarchy!
how should I set the : JsonConfig (setJsonPropertyFilter) properties to get
rid of this error.
Basically there are two options for cycle - write own serializer, use #JsonIgnore. Like in your code - OrderEntity contains list of BidPriceEntities, which in turn contains reference to OrderEntity. Mark ordersBid with #JsonIgnore and it should work. At list serialization... If you don't have access to the class - try mixins.

#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;
}

How to maintain foreign key relationship in hibernate

I have two classes and I want to have a one to many relation between them, for e.g.:
Home(id<int>, rooms<string>)
Vehicle(id<int>, home_id<int>, name<string>)
I need to have a relation between Home and Vehicle class using Home.id and vehicle.home_id.
Please suggest any example which I can use here for CURD operation to implement REST service.
I need to have a relation between Home and Vehicle class using Home.id
and vehicle.home_id.
Your entities should look like this :
Vehicle Entity
#Entity
#Table(name = "vehicle", catalog = "bd_name", schema = "schema_name")
#XmlRootElement
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Column(name = "name")
private String name;
#JoinColumn(name = "home_id", referencedColumnName = "id")
#ManyToOne
private Home homeId;
//constructor getter & setters
}
Home Entity
#Entity
#Table(name = "home", catalog = "bd_name", schema = "schema_name")
#XmlRootElement
public class Home implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Column(name = "room")
private Character room;
#OneToMany(mappedBy = "homeId")
private List<Vehicle> vehicleList;
//constructor getter & setters
}

Categories