My entity class is
#Entity
public class Student_enroll implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int level_num;
private int term;
private String student_session;
private HashMap<String,Integer>mark_value;
#OneToOne
private Student student;
public String getStudent_session() {
return student_session;
}
public void setStudent_session(String student_session) {
this.student_session = student_session;
}
public int getLevel_num() {
return level_num;
}
public void setLevel_num(int level_num) {
this.level_num = level_num;
}
public int getTerm() {
return term;
}
public void setTerm(int term) {
this.term = term;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public HashMap<String, Integer> getMark_value() {
return mark_value;
}
public void setMark_value(HashMap<String, Integer> mark_value) {
this.mark_value = mark_value;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Student_enroll)) {
return false;
}
Student_enroll other = (Student_enroll) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
#Override
public String toString() {
return "com.domain.Student_enroll[ id=" + id + " ]";
}
}
and my controller function is
#RequestMapping(value="/add_mark",method = RequestMethod.POST)
public void add_mark(HttpServletRequest req){
HashMap<String,Integer>map=new HashMap<String,Integer>();
int level=Integer.parseInt(req.getParameter("level"));
int term=Integer.parseInt(req.getParameter("term"));
Student_enroll enroll=student_service.get_student_enroll(level, term);
List<Course>list_course=course_service.list_course(level, term);
Iterator<Course>itr=list_course.iterator();
while(itr.hasNext()){
enroll.put(itr.next().getCourse_Code(),75);
}
enroll.setMark_value(map); // Set hashmap
student_service.update_student_enroll(enroll);
}
I want to set the HashMap by using setHashmap() and want to persist the entity in database.is it an appropriate way cause when I want to persist it other attribute of entity is persisted but the hashmap attribute contains a BLOB object.
How to persist a hashmap of primitive type?
JPA supports Map persistencse using #MapKey, #MapKeyJoinColumn ... annotations
Refer to following article for details:
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Maps
Related
I have actually a problem on a Set in my project (code below)
public static Set<BeanObject> mapToSetBean(Collection<Object> listModel) {
Set<BeanObject> listToReturn = new HashSet<>();
for (Iterator<Object> iterator = listModel.iterator(); iterator.hasNext();) {
Object model = iterator.next();
BeanObject bean = new BeanObject();
bean = mapToBean(model);
listToReturn.add(bean);
}
return listToReturn;
}
When some beans are added to the list they replace another one.
For example:
List{}
add object1 / List{object1}
add object2 / List{object1, object2}
add object1 / List{object1, object2, object3}
add object4 / List{object4, object2, object3}
The equals and the hashcode of the object are override the hashcode are all different and in debug mode we don't enter in the override equals.
When I use an ArrayList everything works but I prefer not to change the type it has a huge impact on my project.
---------------- EDIT ---------------
public static BeanObject mapToBean(Object model) {
BeanObject bean = new BeanObject();
if (model != null) {
bean.setId(model.getId());
if(model.getId() != null){
bean.setIdString(model.getId().toString());
}
if (model.getName() != null) {
bean.setName(model.getName().toLowerCase());
}
bean.setActif(model.getActif());
if (model.getShortName() != null) {
bean.setShortName(model.getShortName().toUpperCase());
}
}
return bean;
}
BeanObject
public class BeanObject implements Comparable<BeanObject> {
/**
* serial
*/
private static final long serialVersionUID = 1L;
private BigInteger id;
private String name;
private String shortName;
private Short actif;
private String idString;
public BeanObject() {
}
public BeanObject(BigInteger id, String libelle) {
this.id = id;
this.name = libelle;
}
/*
* (non-Javadoc)
*
* #see java.lang.Object#equals(java.lang.Object)
*/
#Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof BeanObject)) {
return false;
}
BeanObject other = (BeanObject) o;
boolean result;
if (null == this.id) {
if (null == other.id) {
result = true;
} else {
result = false;
}
} else {
result = this.id.equals(other.id);
}
return result;
}
/*
* (non-Javadoc)
*
* #see java.lang.Object#hashCode()
*/
#Override
public int hashCode() {
return this.id.intValue() * name.hashCode() * shortName.hashCode();
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public Short getActif() {
return actif;
}
public void setActif(Short actif) {
this.actif = actif;
}
public String getIdString() {
return idString;
}
public void setIdString(String idString) {
this.idString = idString;
}
}
Are you certain the BeanObject's hashcode values are unique? That hashcode method seems like it would map a lot of objects to 0 if any of its fields hashed to 0 since it is straight multiplication. I would suggest updating to a more standardized approach for it like the following:
#Override
public int hashCode() {
return Objects.hash(id, name, shortName);
}
Objects.hash is from the java.util package.
If this still doesn't solve the problem, I would double check the hashcode results for each bean object at add time.
I have this model with 2 tables, "Cuenta" and "Movimien". When the merge () method of the Movimien table is called, it also attempts to insert into the Cuenta table. I am not sure if relationships are right. Could someone give me a hand with this?
Below, the model of the Movimien table, which when doing the update triggers the insert in the Cuenta table:
#Entity
#Table(name = "movimien")
public class Movimien implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Date fecha;
private short cuentaid;
private float importe;
private String concepto;
#JoinColumn(name = "cuentaid", insertable = false, updatable = false)
#ManyToOne
private Cuenta cuenta;
public Movimien() {
this.cuenta = new Cuenta();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
public short getCuentaid() {
return cuentaid;
}
public void setCuentaid(short cuentaid) {
this.cuentaid = cuentaid;
}
public float getImporte() {
return importe;
}
public void setImporte(float importe) {
this.importe = importe;
}
public String getConcepto() {
return concepto;
}
public void setConcepto(String concepto) {
this.concepto = concepto;
}
public Cuenta getCuenta() {
return cuenta;
}
public void setCuenta(Cuenta cuenta) {
this.cuenta = cuenta;
}
#Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + this.id;
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Movimien other = (Movimien) obj;
if (this.id != other.id) {
return false;
}
return true;
}}
And the model of the Cuenta table, to which the insert is triggered when an update is made in the Movimien table:
#Entity
#Table(name = "cuentas")
public class Cuenta implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String nombre;
private short tipo;
#OneToMany(mappedBy="cuenta")
private List<Movimien> movimien;
public Cuenta() {
this.nombre="";
this.tipo = 0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public short getTipo() {
return tipo;
}
public void setTipo(short tipo) {
this.tipo = tipo;
}
#Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + this.id;
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Cuenta other = (Cuenta) obj;
if (this.id != other.id) {
return false;
}
return true;
}}
Any idea what may be the reason for this behavior?,
thanks in advance!
Fernando
Persitence.xml
Database
Log of update and insert
I am beginner using java and spring jpa (expecially Jhipster). I want to create object in object like this :
But I always get like this :
property buildingsDTO always empty, this is my code, please correct my code in order I get like first picture.
location.java (Domain)
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
#Column(name = "content_location", nullable = false)
private String content_location;
#OneToMany(mappedBy = "location")
#JsonIgnore
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Building> buildings = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent_location() {
return content_location;
}
public Location content_location(String content_location) {
this.content_location = content_location;
return this;
}
public void setContent_location(String content_location) {
this.content_location = content_location;
}
public Set<Building> getBuildings() {
return buildings;
}
public Location buildings(Set<Building> buildings) {
this.buildings = buildings;
return this;
}
public Location addBuilding(Building building) {
this.buildings.add(building);
building.setLocation(this);
return this;
}
public Location removeBuilding(Building building) {
this.buildings.remove(building);
building.setLocation(null);
return this;
}
public void setBuildings(Set<Building> buildings) {
this.buildings = buildings;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Location location = (Location) o;
if (location.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), location.getId());
}
#Override
public int hashCode() {
return Objects.hashCode(getId());
}
#Override
public String toString() {
return "Location{" +
"id=" + getId() +
", content_location='" + getContent_location() + "'" +
"}";
}}
locationDTO.java
public class LocationDTO implements Serializable {
private Long id;
#NotNull
private String content_location;
private Set<BuildingDTO> buildings = new HashSet<>();
public Set<BuildingDTO> getBuildingsDTO() {
return buildings;
}
public void setBuildingsDTO(Set<BuildingDTO> buildings) {
this.buildings = buildings;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent_location() {
return content_location;
}
public void setContent_location(String content_location) {
this.content_location = content_location;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LocationDTO locationDTO = (LocationDTO) o;
if(locationDTO.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), locationDTO.getId());
}
#Override
public int hashCode() {
return Objects.hashCode(getId());
}
#Override
public String toString() {
return "LocationDTO{" +
"id=" + getId() +
", content_location='" + getContent_location() + "'" +
"}";
}}
locationMapper.java
public interface LocationMapper extends EntityMapper <LocationDTO, Location> {
#Mapping(target = "buildings", ignore = true)
Location toEntity(LocationDTO locationDTO);
default Location fromId(Long id) {
if (id == null) {
return null;
}
Location location = new Location();
location.setId(id);
return location;
}}
buildingDTO.java
public class BuildingDTO implements Serializable {
private Long id;
#NotNull
private String content_building;
private Long locationId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent_building() {
return content_building;
}
public void setContent_building(String content_building) {
this.content_building = content_building;
}
public Long getLocationId() {
return locationId;
}
public void setLocationId(Long locationId) {
this.locationId = locationId;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BuildingDTO buildingDTO = (BuildingDTO) o;
if(buildingDTO.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), buildingDTO.getId());
}
#Override
public int hashCode() {
return Objects.hashCode(getId());
}
#Override
public String toString() {
return "BuildingDTO{" +
"id=" + getId() +
", content_building='" + getContent_building() + "'" +
"}";
}}
please anyone help me.
thanks.
By default jHipster will mark any OneToMany entity relationships as #JsonIgnore so that the Set of buildings is not returned in the JSON:
#OneToMany(mappedBy = "location")
#JsonIgnore
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Building> buildings = new HashSet<>();
If you want this to show up in the JSON then you should remove that annotation and also mark it with an eager loading strategy so that the set of buildings are loaded as you expect:
#OneToMany(mappedBy = "location", fetch = FetchType.EAGER)
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Building> buildings = new HashSet<>();
This question already has answers here:
Why do I need to override the equals and hashCode methods in Java?
(31 answers)
Hashcode and Equals for Hashset [duplicate]
(5 answers)
Closed 5 years ago.
There is an entity :
#Entity
#Table(name = "menu")
public class Menu {
#Id
#SequenceGenerator(name="s_menu", sequenceName="s_menu", allocationSize=1)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_menu")
#Column(name = "menu_id")
private int id;
#Column(name = "gmnu_code")
private Integer groupeMenu;
#Column(name = "menu_lib")
private String lib;
#Column(name = "menu_ordre")
private Integer ordre;
#Column(name = "menu_visible")
private Integer visible;
#Column(name = "menu_deleted")
private Integer deleted;
#Column(name = "menu_bouton")
private Integer bouton;
#Column(name = "menu_parent")
private Integer parent;
#Column(name = "menu_controlleur")
private String controlleur;
#Column(name = "menu_navigation")
private String navigation;
#Column(name = "menu_flag_trace")
private Integer flag_trace;
#Column(name = "menu_trace")
private String trace;
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "menus")
#JsonBackReference
private Set<Role> roles = new HashSet<Role>();
#Transient
private int depth;
#Transient
private boolean affectedToARole;
#Transient
private Environment env;
public Menu() {
super();
}
public Menu(Integer gmnu_code, String menu_lib, Integer menu_ordre, Integer menu_visible, Integer menu_deleted, Integer menu_bouton, Integer menu_parent, String menu_controlleur, String menu_navigation, Integer menu_flag_trace, String menu_trace) {
super();
this.groupeMenu = gmnu_code;
this.lib = menu_lib;
this.ordre = menu_ordre;
this.visible = menu_visible;
this.deleted = menu_deleted;
this.bouton = menu_bouton;
this.parent = menu_parent;
this.controlleur = menu_controlleur;
this.navigation = menu_navigation;
this.flag_trace = menu_flag_trace;
this.trace = menu_trace;
}
public Menu(Integer gmnu_code, String menu_lib, Integer menu_ordre, Integer menu_visible, Integer menu_deleted, Integer menu_bouton, Integer menu_parent, String menu_controlleur, String menu_navigation, Integer menu_flag_trace, String menu_trace, Set<Role> roles) {
super();
this.groupeMenu = gmnu_code;
this.lib = menu_lib;
this.ordre = menu_ordre;
this.visible = menu_visible;
this.deleted = menu_deleted;
this.bouton = menu_bouton;
this.parent = menu_parent;
this.controlleur = menu_controlleur;
this.navigation = menu_navigation;
this.flag_trace = menu_flag_trace;
this.trace = menu_trace;
this.roles = roles;
}
public int getId() {
return id;
}
public void setId(int menu_id) {
this.id = menu_id;
}
public Integer getGroupeMenu() {
return groupeMenu;
}
public void setGroupeMenu(Integer gmnu_code) {
this.groupeMenu = gmnu_code;
}
public String getLib() {
if (this.env != null)
return env.getProperty(lib.trim());
return lib;
}
public void setLib(String menu_lib) {
this.lib = menu_lib;
}
public Integer getOrdre() {
return ordre;
}
public void setOrdre(Integer menu_ordre) {
this.ordre = menu_ordre;
}
public Integer getVisible() {
return visible;
}
public void setVisible(Integer menu_visible) {
this.visible = menu_visible;
}
public Integer getDeleted() {
return deleted;
}
public void setDeleted(Integer menu_deleted) {
this.deleted = menu_deleted;
}
public Integer getBouton() {
return bouton;
}
public void setBouton(Integer menu_bouton) {
this.bouton = menu_bouton;
}
public Integer getParent() {
return parent;
}
public void setParent(Integer menu_parent) {
this.parent = menu_parent;
}
public String getControlleur() {
return controlleur;
}
public void setControlleur(String menu_controlleur) {
this.controlleur = menu_controlleur;
}
public String getNavigation() {
try {
if (this.env != null)
return env.getProperty(navigation.trim());
return navigation.trim();
} catch (NullPointerException npe) {
return " --- ";
}
}
public void setNavigation(String menu_navigation) {
this.navigation = menu_navigation;
}
public String getTrace() {
if (this.env != null)
return HtmlUtils.htmlUnescape(env.getProperty(trace));
return trace;
}
public void setTrace(String menu_trace) {
this.trace = HtmlUtils.htmlUnescape(menu_trace);
}
public Integer getFlag_trace() {
return flag_trace;
}
public void setFlag_trace(Integer menu_flag_trace) {
this.flag_trace = menu_flag_trace;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public void setEnv(Environment env) {
this.env = env;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public boolean isAffectedToARole() {
return affectedToARole;
}
public void setAffectedToARole(boolean affectedToARole) {
this.affectedToARole = affectedToARole;
}
#Override
public boolean equals(Object obj) { // pour pouvoir utiliser la méthode "contains" sur une liste de Menu
if (this == null && obj == null)
return true;
if (this != null && obj != null && obj instanceof Menu)
return id == ((Menu)obj).getId();
return super.equals(obj);
}
}
I want to test if a Set of Menu's contains a particular Menu :
Set<Menu> affected_menus = roleDao.getListMenu(role_code); // gets all menus affected to a role_code
for (Menu menu : temp) {
if (!affected_menus.isEmpty() && affected_menus.contains(menu))
menu.setAffectedToARole(true);
else
menu.setAffectedToARole(false);
}
At runtime all code goes to menu.setAffectedToARole(false);
So what is wrong ?
When overriding equals you must always override hashCode as well. Both methods are used together across Java Collections, etc.
In your case, this implementation might be enough:
#Override
public int hashCode() {
return id;
}
Also, your equals implementation could use some help as well:
#Override
public boolean equals(Object obj) {
if (obj instanceof Menu)
return id == ((Menu)obj).getId();
return false;
}
The javadoc of the the int hashCode() method from the Object class explains that :
it returns a hash code value for the object. This method is supported
for the benefit of hash tables such as those provided by
java.util.HashMap.
The hashCode() method intention is clear : being using in collections relying on hash tables : HashMap, HashSet, LinkedHashMap, etc…
Besides, the general contract of hashcode() mentions the relation between equals() and hashcode() :
If two objects are equal according to the equals(Object) method, then
calling the hashCode method on each of the two objects must produce
the same integer result.
So to be able to use your custom class with a HashSet, you need to override both equals() and hashcode() in a consistent way.
you should also override hashCode() method
here some info: Why do I need to override the equals and hashCode methods in Java?
I am trying to insert data in oracle DB using spring JPA repositories
I have a hash map which contains all the values which needs to be populated into DB,I am Iterating each value and setting into my Entity class
Basically I have a table which has a composite primary key(NotifiedToId).when I am setting the values ints throwing constraint violation exception.In my logs it is printing all correct values but its not getting inserted,
My Entity class:
#Embeddable
public class TbBamiNotifUserLogPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
#Column(name="NOTIF_REF_NO")
private String notifRefNo;
#Column(name="NOTIFIED_TO_ID")
private String notifiedToId;
public TbBamiNotifUserLogPK() {
}
public String getNotifRefNo() {
return this.notifRefNo;
}
public void setNotifRefNo(String notifRefNo) {
this.notifRefNo = notifRefNo;
}
public String getNotifiedToId() {
return this.notifiedToId;
}
public void setNotifiedToId(String notifiedToId) {
this.notifiedToId = notifiedToId;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof TbBamiNotifUserLogPK)) {
return false;
}
TbBamiNotifUserLogPK castOther = (TbBamiNotifUserLogPK)other;
return
this.notifRefNo.equals(castOther.notifRefNo)
&& this.notifiedToId.equals(castOther.notifiedToId);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.notifRefNo.hashCode();
hash = hash * prime + this.notifiedToId.hashCode();
return hash;
}
}
import java.io.Serializable;
import javax.persistence.*;
#Entity
#Table(name="TB_BAMI_NOTIF_USER_LOG")
#NamedQuery(name="TbBamiNotifUserLog.findAll", query="SELECT t FROM TbBamiNotifUserLog t")
public class TbBamiNotifUserLog implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private TbBamiNotifUserLogPK id;
#Column(name="NOTIF_CAT")
private String notifCat;
#Column(name="NOTIFIED_TO_NAME")
private String notifiedToName;
#Column(name="NOTIFIED_TO_ROLE")
private String notifiedToRole;
public TbBamiNotifUserLog() {
}
public TbBamiNotifUserLogPK getId() {
return this.id;
}
public void setId(TbBamiNotifUserLogPK id) {
this.id = id;
}
public String getNotifCat() {
return this.notifCat;
}
public void setNotifCat(String notifCat) {
this.notifCat = notifCat;
}
public String getNotifiedToName() {
return this.notifiedToName;
}
public void setNotifiedToName(String notifiedToName) {
this.notifiedToName = notifiedToName;
}
public String getNotifiedToRole() {
return this.notifiedToRole;
}
public void setNotifiedToRole(String notifiedToRole) {
this.notifiedToRole = notifiedToRole;
}
}
#Entity
#Table(name="TB_BAMI_NOTIFICATION_LOG")
#NamedQuery(name="TbBamiNotificationLog.findAll", query="SELECT t FROM TbBamiNotificationLog t")
public class TbBamiNotificationLog implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="NOTIF_REF_NO")
private String notifRefNo;
#Column(name="\"ACTION\"")
private String action;
#Column(name="NOTIF_CONTENT")
private String notifContent;
#Column(name="NOTIF_TYPE")
private String notifType;
#Column(name="NOTIFIED_DATE_TIME")
private Timestamp notifiedDateTime;
private String refno;
#Column(name="\"VERSION\"")
private BigDecimal version;
public TbBamiNotificationLog() {
}
public String getNotifRefNo() {
return this.notifRefNo;
}
public void setNotifRefNo(String notifRefNo) {
this.notifRefNo = notifRefNo;
}
public String getAction() {
return this.action;
}
public void setAction(String action) {
this.action = action;
}
public String getNotifContent() {
return this.notifContent;
}
public void setNotifContent(String notifContent) {
this.notifContent = notifContent;
}
public String getNotifType() {
return this.notifType;
}
public void setNotifType(String notifType) {
this.notifType = notifType;
}
public Timestamp getNotifiedDateTime() {
return this.notifiedDateTime;
}
public void setNotifiedDateTime(Timestamp notifiedDateTime) {
this.notifiedDateTime = notifiedDateTime;
}
public String getRefno() {
return this.refno;
}
public void setRefno(String refno) {
this.refno = refno;
}
public BigDecimal getVersion() {
return this.version;
}
public void setVersion(BigDecimal version) {
this.version = version;
}
}
Business Logic:
for (Entry<String, PushNotificationDetails> entry : finalTemplate.entrySet()){
try{
notificationlog.setNotifRefNo("121323");
notificationlog.setRefno(refNum);
notificationlog.setVersion(new BigDecimal(1));
notificationlog.setAction(action);
LOGGER.debug("TEMPLATE TYPE"+entry.getValue().getTemplate_type());
LOGGER.debug("TEMPLATE"+entry.getValue().getTemplate());
notificationlog.setNotifType(entry.getValue().getTemplate_type());
notificationlog.setNotifContent(entry.getValue().getTemplate());
notificationlog.setNotifiedDateTime(notifiedDateTime);
tbBamiNotifyLogRepository.save(notificationlog);
LOGGER.debug("inside if block ::: ");
LOGGER.debug("USERID is: "+entry.getValue().getUserID());
LOGGER.debug("NOTIFCAT is: "+entry.getValue().getNotif_cat());
LOGGER.debug("NOTIFUSER is: "+entry.getValue().getUserName());
LOGGER.debug("NOTIFROLE is: "+entry.getKey());
tbBamiNotifUserLogPK.setNotifiedToId(entry.getValue().getUserID());
tbBamiNotifUserLogPK.setNotifRefNo("121323");
tbBamiNotifUserLog.setId(tbBamiNotifUserLogPK);
LOGGER.debug("GET is: "+tbBamiNotifUserLog.getId().getNotifiedToId());
tbBamiNotifUserLog.setNotifCat(entry.getValue().getNotif_cat());
tbBamiNotifUserLog.setNotifiedToName(entry.getValue().getUserName());
tbBamiNotifUserLog.setNotifiedToRole(entry.getKey());
tbBamiNotifyUserLogRepository.save(tbBamiNotifUserLog);
}
Try to add notificationlog = new TbBamiNotificationLog() in the beginning of your saving for. It could be possible when you try to save the second row but the instance is the same (with id provided during the first save).