I have two tables - users and friends.
Any user may have any number of friends (another users). These relationships are stored in a table 'friends', so this is just service table.
Pricture of relationships
How should I code this in my entity classes? Should I use #ManyToMany?
My presumption is the next:
User.java:
#Entity
#Table(name = "users")
#NoArgsConstructor
public class User implements Serializable {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name = "id")
private Long id;
#Basic
#Column(name = "name", nullable = false, length = 45)
private String name;
#ManyToMany(cascade = { CascadeType.ALL })
#JoinTable(
name = "friends",
joinColumns = { #JoinColumn(name = "id") },
inverseJoinColumns = { #JoinColumn(name = "sender") }
)
private Set<User> friendsInviters = new HashSet<>();
#ManyToMany(mappedBy = "users")
private Set<User> friendsReceiver = new HashSet<>();
}
Friend.java:
#Entity
#Table(name = "friends")
#NoArgsConstructor
public class Friend implements Serializable {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name = "id")
private Long id;
#Basic
#Column(name = "sender")
private Long sender;
#Basic
#Column(name = "receiver")
private Long receiver;
}
P.S. - i just started to learn Hibernate:)
#Entity
#Table(name = "users")
public class User implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
#Column(name = "name", nullable = false, length = 45)
private String name;
#ManyToMany(cascade = { CascadeType.ALL })
#JoinTable(name="users_friends",
joinColumns = { #JoinColumn(name = "user_id") },
inverseJoinColumns = { #JoinColumn(name = "friend_id") }
)
private Set<User> friendsInviters = new HashSet<>();
#ManyToMany(mappedBy = "friendsInviters")
private Set<User> friendsReceiver = new HashSet<>();
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<User> getFriendsInviters() {
return friendsInviters;
}
public void setFriendsInviters(Set<User> friendsInviters) {
this.friendsInviters = friendsInviters;
}
public Set<User> getFriendsReceiver() {
return friendsReceiver;
}
public void setFriendsReceiver(Set<User> friendsReceiver) {
this.friendsReceiver = friendsReceiver;
}
}
I have a problem with my multiselect. I'm using it as an Specification with multiple joins. I don't know why hibernate decides to put a dot in the query. the error is shown below in the query. this is the constructor I'm using
public HelperOutput(Long id,
String personName,
Long helperTypeId,
String helperTypeTitle,
String agencyTitle,
String regionalOfficeTitle,
String basketPartTitle,
YesNo allowedToOperate,
YesNo hasFlatTirePack,
Long fleetRescueId,
Long cityId,
String cityName,
Long skillLevel,
String helperRankTitle,
Long personId,
Long helperRate,
String personelId,
Long regionalOfficeId,
Long basketPartId,
String mobile,
String fleetName,
ExpertType expertType,
Long maxConcurrentWork,
Set<Long> dutyCityIds
) {
this.id = id;
this.helperTypeId = helperTypeId;
this.helperTypeTitle = helperTypeTitle;
this.agencyTitle = agencyTitle;
this.regionalOfficeId = regionalOfficeId;
this.regionalOfficeTitle = regionalOfficeTitle;
this.basketPartTitle = basketPartTitle;
this.fleetRescueId = fleetRescueId;
this.cityId = cityId;
this.helperRankTitle = helperRankTitle;
this.personId = personId;
this.basketPartId = basketPartId;
this.mobile = mobile;
this.fleetName = fleetName;
this.cityName = cityName;
this.personName = personName;
this.allowedToOperate = allowedToOperate;
this.hasFlatTirePack = hasFlatTirePack;
this.skillLevel = skillLevel;
this.helperRate = helperRate;
this.personelId = personelId;
this.expertType = expertType;
this.maxConcurrentWork = maxConcurrentWork;
this.dutyCityIds = dutyCityIds;
}
this is the multiselect which is similar to the constructor.
cq.multiselect(root.get(Helper_.ID),
personName,
helperHelperTypeJoin.get(HelperType_.ID),
helperHelperTypeJoin.get(HelperType_.TITLE),
helperAgencyJoin.get(Agency_.TITLE),
helperRegionalOfficeJoin.get(RegionalOffice_.TITLE)
, helperBasketPartJoin.get(Basket_.TITLE),
root.get(Helper_.ALLOWED_TO_OPERATE),
root.get(Helper_.HAS_FLAT_TIRE_PACK),
fleetRescueJoin.get(HelperFleet_.ID),
helperCityJoin.get(City_.ID) ,
helperCityJoin.get(City_.NAME),
root.get(Helper_.SKILL_LEVEL),
helperRankJoin.get(HelperRank_.NAME),
helperPersonJoin.get(Person_.ID),
root.get(Helper_.helperRate),
root.get(Helper_.personelId),
helperRegionalOfficeJoin.get(RegionalOffice_.ID),
helperBasketPartJoin.get(Basket_.ID),
helperPersonJoin.get(Person_.MOBILE),
HelperFleetJoin.get(Fleet_.NAME),
root.get(Helper_.EXPERT_TYPE),
root.get(Helper_.MAX_CONCURRENT_WORK),
root.get(Helper_.DUTY_CITIES)
);
this is the hibernate query which I don't know why it has a dot.
select
*
from
( select
helper0_.ID as col_0_0_,
person3_.NAME as col_1_0_,
helpertype7_.ID as col_2_0_,
helpertype7_.TITLE as col_3_0_,
agency2_.TITLE as col_4_0_,
regionalof8_.TITLE as col_5_0_,
basket6_.TITLE as col_6_0_,
helper0_.ALLOWED_TO_OPERATE as col_7_0_,
helper0_.HAS_FLAT_TIRE_PACK as col_8_0_,
helperflee4_.ID as col_9_0_,
city1_.ID as col_10_0_,
city1_.NAME as col_11_0_,
helper0_.SKILL_LEVEL as col_12_0_,
helperrank9_.NAME as col_13_0_,
person3_.ID as col_14_0_,
helper0_.HELPER_RATE as col_15_0_,
helper0_.PERSONELID as col_16_0_,
regionalof8_.ID as col_17_0_,
basket6_.ID as col_18_0_,
person3_.MOBILE as col_19_0_,
fleet5_.NAME as col_20_0_,
helper0_.EXPERT_TYPE as col_21_0_,
helper0_.MAX_CONCURRENT_WORK as col_22_0_,
. as col_23_0_ <----------- this causes the problem
from
OPR.TBL_HELPER helper0_
left outer join
OPR.TBL_CITY city1_
on helper0_.CITY_ID=city1_.ID
left outer join
TBL_AGENCY agency2_
on helper0_.AGENCY_ID=agency2_.ID
left outer join
OPR.TBL_PERSONS person3_
on helper0_.PERSON_ID=person3_.ID
left outer join
OPR.TBL_HELPER_FLEET helperflee4_
on helper0_.HELPER_FLEET_ID=helperflee4_.ID
left outer join
OPR.NOEKHODROEMDAD fleet5_
on helperflee4_.NOEKHODROEMDAD_ID=fleet5_.NOEKHODROEMDADID
left outer join
OPR.TBL_BASKET basket6_
on helper0_.BASKET_PART_ID=basket6_.ID
left outer join
OPR.TBL_HELPER_TYPE helpertype7_
on helper0_.HELPER_TYPE_ID=helpertype7_.ID
left outer join
TBL_REGIONAL_OFFICE regionalof8_
on helper0_.REGIONAL_OFFICE_ID=regionalof8_.ID
left outer join
OPR.TBL_HELPER_RANK helperrank9_
on helper0_.RANK_HELPER_ID=helperrank9_.ID
left outer join
TBL_HELPER_DUTY_CITIES dutycities10_
on helper0_.ID=dutycities10_.DUTY_CITY_ID
left outer join
OPR.TBL_CITY city11_
on dutycities10_.ESTABLISHMENT_CITY_ID=city11_.ID
inner join
TBL_HELPER_DUTY_CITIES dutycities12_
on helper0_.ID=dutycities12_.DUTY_CITY_ID
where
1=1
and 1=1
order by
helper0_.ALLOWED_TO_OPERATE asc,
person3_.NAME asc )
where
rownum <= ?
and at last I get this error when returning an Specification from the predicates I made.
Error:
2022-01-31 15:40:08.419 [http-nio-8082-exec-2] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00936: missing expression
2022-01-31 15:40:08.444 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> #AfterThrowing method: GenericRepository.findAll(..)
2022-01-31 15:40:08.445 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> detailedMessage: ORA-00936: missing expression
2022-01-31 15:40:08.445 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> detailedCause: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
2022-01-31 15:40:08.445 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> #AfterThrowing method: HelperServiceImpl.findByCriteria(..)
2022-01-31 15:40:08.445 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> detailedMessage: ORA-00936: missing expression
2022-01-31 15:40:08.445 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> detailedCause: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
2022-01-31 15:40:08.446 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> #AfterThrowing method: HelperController.show(..)
2022-01-31 15:40:08.446 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> detailedMessage: ORA-00936: missing expression
2022-01-31 15:40:08.446 [http-nio-8082-exec-2] ERROR com.eki.opr.config.LoggingAspect - ----> detailedCause: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
EDIT
these are the entities:
#Entity(name = "HELPER")
#Table(name = "TBL_HELPER", schema = "OPR")
#EqualsAndHashCode(exclude = {"person", "agency", "helperType", "regionalOffice", "basket_part", "helperRank"})
#ToString(exclude = {"person", "agency", "helperType", "regionalOffice", "basket_part", "helperRank"})
#AllArgsConstructor
public class Helper implements Serializable {
private Long id;
#GenericGenerator(
name = "assigned-sequence",
strategy = "com.eki.opr.utils.StringSequenceIdentifier"
)
#GeneratedValue(
generator = "assigned-sequence",
strategy = GenerationType.AUTO)
private String largeId;
private Long skillLevel;
private Long currentWorkCount;
private Long maxConcurrentWork;
private YesNo allowedToOperate;
private YesNo hasGps;
private YesNo isAutoDialer;
private YesNo isPartSupply;
private YesNo hasFlatTirePack;
private String imei;
private String address;
private String personelId;
private String reservedMobile;
private Person person;
private HelperFleet helperFleet;
private HelperType helperType;
private RegionalOffice regionalOffice;
private Agency agency;
private Basket basket_part;
private Basket basket_tools;
private YesNo hasPos;
private String posSerial;
private HelperRank helperRank;
private Parameter contractType;
private Set<ServiceType> serviceTypes = new HashSet<ServiceType>();
private Long helperRate;
private ExpertType expertType = ExpertType.HELPER;
public Helper() {
}
private City city;
#JoinColumn(name = "CITY_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
private Set<HelperDutyCity> dutyCities = new HashSet<>();
/* in this part mappedBy's value should be changed to helper because the
join must be on helper not dutyCityId*/
#OneToMany(targetEntity = HelperDutyCity.class, mappedBy = "dutyCityId", fetch = FetchType.EAGER)
public Set<HelperDutyCity> getDutyCities() {
return dutyCities;
}
public void setDutyCities(Set<HelperDutyCity> dutyCities) {
this.dutyCities = dutyCities;
}
public void setHelperRate(Long helperRate) {
this.helperRate = helperRate;
}
#Column(name = "HELPER_RATE")
public Long getHelperRate() {
return helperRate;
}
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_helper")
#SequenceGenerator(name = "seq_helper", sequenceName = "SA.SEQ_EMDADGAR_ID", allocationSize = 1,initialValue = 63442)
#Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "ADDRESS")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
#Column(name = "SKILL_LEVEL")
public Long getSkillLevel() {
return skillLevel;
}
public void setSkillLevel(Long skillLevel) {
this.skillLevel = skillLevel;
}
#Column(name = "MAX_CONCURRENT_WORK")
public Long getMaxConcurrentWork() {
return maxConcurrentWork;
}
public void setMaxConcurrentWork(Long maxConcurrentWork) {
this.maxConcurrentWork = maxConcurrentWork;
}
#Column(name = "CURRENT_WORK_COUNT")
public Long getCurrentWorkCount() {
return currentWorkCount;
}
public void setCurrentWorkCount(Long currentWorkCount) {
this.currentWorkCount = currentWorkCount;
}
#Column(name = "ALLOWED_TO_OPERATE")
#Convert(converter = YesNoConverter.class)
public YesNo getAllowedToOperate() {
return allowedToOperate;
}
public void setAllowedToOperate(YesNo allowedToOperate) {
this.allowedToOperate = allowedToOperate;
}
#Column(name = "HAS_GPS")
#Convert(converter = YesNoConverter.class)
public YesNo getHasGps() {
return hasGps;
}
public void setHasGps(YesNo hasGps) {
this.hasGps = hasGps;
}
#Column(name = "PART_SUPPLY")
#Convert(converter = YesNoConverter.class)
public YesNo getIsPartSupply() {
return isPartSupply;
}
public void setIsPartSupply(YesNo isPartSupply) {
this.isPartSupply = isPartSupply;
}
#Column(name = "PERSONELID")
public String getPersonelId() {
return personelId;
}
public void setPersonelId(String personelId) {
this.personelId = personelId;
}
#Column(name = "HAS_FLAT_TIRE_PACK")
#Convert(converter = YesNoConverter.class)
public YesNo getHasFlatTirePack() {
return hasFlatTirePack;
}
public void setHasFlatTirePack(YesNo hasFlatTirePack) {
this.hasFlatTirePack = hasFlatTirePack;
}
#Column(name = "IMEI")
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
#JoinColumn(name = "CREATED_BY", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
private PersonUnion createdBy;
#Column(name = "CREATED_AT")
private LocalDateTime createdAt;
#Column(name = "IS_AUTO_DIALER")
#Convert(converter = YesNoConverter.class)
public YesNo getIsAutoDialer() {
return isAutoDialer;
}
public void setIsAutoDialer(YesNo isAutoDialer) {
this.isAutoDialer = isAutoDialer;
}
#Column(name = "RESERVED_MOBILE")
public String getReservedMobile() {
return reservedMobile;
}
#Column(name = "HAS_POS")
#Convert(converter = YesNoConverter.class)
public YesNo getHasPos() { return hasPos; }
public void setHasPos(YesNo hasPos) {
this.hasPos = hasPos;
}
#Column(name = "POS_SERIAL")
public String getPosSerial() { return posSerial; }
public void setPosSerial(String posSerial) {
this.posSerial = posSerial;
}
public void setReservedMobile(String reservedMobile) {
this.reservedMobile = reservedMobile;
}
#JoinColumn(name = "MODIFIED_BY", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
private PersonUnion modifiedBy;
#Column(name = "MODIFIED_AT")
private LocalDateTime modifiedAt;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "PERSON_ID", referencedColumnName = "ID")
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
#JoinColumn(name = "HELPER_TYPE_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public HelperType getHelperType() {
return helperType;
}
public void setHelperType(HelperType helperType) {
this.helperType = helperType;
}
#JoinColumn(name = "REGIONAL_OFFICE_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public RegionalOffice getRegionalOffice() {
return regionalOffice;
}
public void setRegionalOffice(RegionalOffice regionalOffice) {
this.regionalOffice = regionalOffice;
}
#JoinColumn(name = "AGENCY_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public Agency getAgency() {
return agency;
}
public void setAgency(Agency agency) {
this.agency = agency;
}
#JoinColumn(name = "BASKET_PART_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public Basket getBasket_part() {
return basket_part;
}
public void setBasket_part(Basket basket_part) {
this.basket_part = basket_part;
}
#JoinColumn(name = "BASKET_TOOLS_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public Basket getBasket_tools() {
return basket_tools;
}
public void setBasket_tools(Basket basket_tools) {
this.basket_tools = basket_tools;
}
#ManyToOne
#JoinColumn(name = "HELPER_FLEET_ID", referencedColumnName = "ID")
public HelperFleet getHelperFleet() {
return helperFleet;
}
public void setHelperFleet(HelperFleet helperFleet) {
this.helperFleet = helperFleet;
}
#JoinColumn(name = "RANK_HELPER_ID", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.LAZY)
public HelperRank getHelperRank() {
return helperRank;
}
public void setHelperRank(HelperRank helperRank) {
this.helperRank = helperRank;
}
#Column(name = "LARGE_ID")
public String getLargeId(){return this.largeId;}
public void setLargeId(String largeId){this.largeId = largeId;}
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "TBL_HELPER_SERVICE_TYPE", uniqueConstraints = {#UniqueConstraint(columnNames = {"HELPER_ID", "SERVICE_TYPE_ID"})},
joinColumns = #JoinColumn(name = "HELPER_ID", referencedColumnName = "ID"),
inverseJoinColumns = #JoinColumn(name = "SERVICE_TYPE_ID", referencedColumnName = "ID"))
public Set<ServiceType> getServiceTypes() {
return serviceTypes;
}
public void setServiceTypes(Set<ServiceType> serviceTypes) {
this.serviceTypes = serviceTypes;
}
#JoinColumn(name = "CONTRACT_TYPE")
#ManyToOne
public Parameter getContractType() {
return contractType;
}
public void setContractType(Parameter contractType){
this.contractType = contractType;
}
#Convert(converter = ExpertTypeConverter.class)
#Column(name = "EXPERT_TYPE")
public ExpertType getExpertType() {
return expertType;
}
public void setExpertType(ExpertType expertType) {
this.expertType = expertType;
}
public void fromDto(HelperInput input) {
this.id = input.getId();
this.address = input.getAddress();
this.allowedToOperate = input.getAllowedToOperate();
this.currentWorkCount = input.getCurrentWorkCount();
this.hasGps = input.getHasGps();
this.hasFlatTirePack = input.getHasFlatTirePack();
this.isAutoDialer = input.getIsAutoDialer();
this.personelId = input.getPersonelId();
this.isPartSupply = input.getIsPartSupply();
this.maxConcurrentWork = input.getMaxConcurrentWork();
this.hasPos = input.getHasPos();
this.posSerial = input.getPosSerial();
this.skillLevel = input.getSkillLevel();
this.reservedMobile = input.getReservedMobile();
if (input.getId() == null) { //first time
this.largeId = StringSequenceIdentifier.generateLargeId();
this.createdBy = input.getCreatedByEntity();
this.createdAt = LocalDateTime.now();
this.modifiedBy = input.getCreatedByEntity();
this.modifiedAt = createdAt;
} else {
this.modifiedBy = input.getModifiedByEntity();
this.modifiedAt = LocalDateTime.now();
}
this.helperRate = input.getHelperRate();
}
}
#Entity
#Getter
#Setter
#NoArgsConstructor
#ToString
#AllArgsConstructor
#JsonDeserialize
#Table(name = "TBL_HELPER_DUTY_CITIES")
#IdClass(HelperDutyCityKey.class)
public class HelperDutyCity implements Serializable {
#Id
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "HELPER_ID")
private Helper helper;
#Id
#OneToOne
#JoinColumn(name = "ESTABLISHMENT_CITY_ID", referencedColumnName = "ID")
#Fetch(FetchMode.JOIN)
private City establishmentCityId;
#ManyToOne
#JoinColumn(name = "DUTY_CITY_ID", referencedColumnName = "ID")
#Fetch(FetchMode.JOIN)
private City dutyCityId;
public HelperDutyCity(Helper helper, City establishmentCityId) {
this.helper = helper;
this.establishmentCityId = establishmentCityId;
}
}
#Data
#ToString(exclude = {"regions","regionalOffice", "province","productCities"})
#EqualsAndHashCode(exclude = {"regions","regionalOffice", "province","productCities"})
#JsonInclude(JsonInclude.Include.NON_NULL)
#Entity
#Table(name = "TBL_CITY", schema = "OPR")
public class City implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#GenericGenerator(
name = "seqCity",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
#org.hibernate.annotations.Parameter(name = "sequence_name", value = "SEQ_CITY"),
#org.hibernate.annotations.Parameter(name = "initial_value", value = "100219"),
#org.hibernate.annotations.Parameter(name = "increment_size", value = "1")
}
)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqCity")
#Id
#Column(name = "ID")
private Long id;
#Column(name = "NAME")
private String name;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "PROVINCE_ID", nullable = false)
private Province province;
#GenericGenerator(
name = "assigned-sequence",
strategy = "com.eki.opr.utils.StringSequenceIdentifier"
)
#GeneratedValue(
generator = "assigned-sequence",
strategy = GenerationType.SEQUENCE)
#Column(name = "LARGE_ID")
private String largeId;
#Column(name = "OPR_SHAHR_ID")
private Double oprCityId;
#Column(name = "LATITUDE")
private Double latitude;
#Column(name = "LONGITUDE")
private Double longitude;
#Column(name = "METROPOLIS_FACTOR")
private Double metropolisFactor;
#Convert(converter = YesNoConverter.class)
#Column(name = "IS_AUTOMATED_DISPATCH")
private YesNo isAutomatedDispatch;
#Convert(converter = GeneralStatusConverter.class)
#Column(name = "STATUS")
private GeneralStatus status;
#Column(name = "PARSIMAP_CITY_CODE")
private String parsiMapCityCode;
#Column(name = "AMAR_CITY_CODE")
private String parsiMapCityAmarCode;
#JoinColumn(name = "REGIONAL_OFFICE_ID")
#ManyToOne(fetch = FetchType.LAZY)
private RegionalOffice regionalOffice;
#ManyToMany(
fetch = FetchType.LAZY, mappedBy = "cities")
Set<Region> regions = new HashSet<>();
/* #OneToMany(mappedBy = "city")
private Set<Address> addresses;*/
public City() {
}
public City(Long id) {
this.id = id;
// this.addresses = addresses;
}
public City(CityDto dto) {
fromDto(dto);
}
public void fromDto(CityDto cityDto) {
this.name = cityDto.getName();
this.metropolisFactor = cityDto.getMetropolisFactor();
}
public CityDto toDto() {
CityDto dto = new CityDto();
dto.setId(id);
dto.setName(name);
dto.setMetropolisFactor(metropolisFactor);
dto.setProvinceDto(province.toDto());
dto.setRegionalOfficeOutput(regionalOffice.toDto());
return dto;
}
public CityLite toDtoLite() {
CityLite dto = new CityLite();
dto.setId(id);
dto.setName(name);
dto.setMetropolisFactor(metropolisFactor);
dto.setProvinceId(province != null ? province.getId() : null);
dto.setProvinceName(province != null ? province.getName() : "");
dto.setRegionalOfficeId(regionalOffice != null ? regionalOffice.getId() : null);
dto.setRegionalOfficeTitle(regionalOffice != null ? regionalOffice.getTitle() : "");
dto.setParsiMapCityCode(parsiMapCityCode);
dto.setParsiMapAmarCityCode(parsiMapCityAmarCode);
dto.setLat(latitude != null ? latitude :0);
dto.setLng(longitude != null ? longitude :0);
return dto;
}
}
I answered it in a comment in Helper entity. the answer is you shouldn't make a mistake in mappedBy attribute in #OneToMany annotation. so I changed dutyCityId to helper now it's working.
before it was joining with another condition helper.ID = helperDutyCity.dutyCityId which was wrong now it joins with this condition helper.ID = helperDutyCity.helperId which is correct.
I am trying to store a manytomany relationship but it not stores the relationship.
The following code has generated 3 tables.
Soldier, medal and soldier_medals.
The service just make a call to a CRUD Interface with save(soldier).
It stores correctly the soldier, but it not fill any row at the soldier_medals table.
This is the JSON I send to the server:
{
"abbreviatedSequence": "XDF",
"medals": [
{
"name": "Purple",
"id": 1
},
{
"name": "Red",
"id": 2
}
],
"sequence": "XDFREE",
"name": "Savier"
}
Employee.java
#Entity(name="employees")
#Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Employee {
#Id
#GeneratedValue(generator="increment")
#GenericGenerator(name="increment", strategy = "increment")
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Soldier.java
#Entity(name="soldier")
public class Soldier extends Employee{
private String sequence;
private String abbreviatedSequence;
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "soldiers")
private List<Medal> medals;
public List<Medal> getMedals() {
return medals;
}
public void setMedals(List<Medal> medals) {
this.medals = medals;
}
public String getSequence() {
return sequence;
}
public void setSequence(String sequence) {
this.sequence = sequence;
}
public String getAbbreviatedSequence() {
return abbreviatedSequence;
}
public void setAbbreviatedSequence(String abbreviatedSequence) {
this.abbreviatedSequence = abbreviatedSequence;
}
}
Medal.java
#Entity
#Table(name = "medal")
public class Medal {
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.AUTO, generator = "medal_seq")
#SequenceGenerator(name = "medal_seq", sequenceName = "medal_seq", allocationSize = 1)
private Long id;
#Column(name = "NAME", length = 50, unique = true)
#NotNull
#Size(min = 3, max = 50)
private String name;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(name = "soldier_medals", joinColumns = {
#JoinColumn(name = "medal_id", nullable = false, updatable = false) },
inverseJoinColumns = { #JoinColumn(name = "soldier_id",
nullable = false, updatable = false) })
private List<Soldier> soldiers;
public List<Soldier> getSoldiers() {
return soldiers;
}
public void setSoldiers(List<Soldier> soldiers) {
this.soldiers = soldiers;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
SoldierController.java
#RestController
public class SoldierController {
#Autowired
private SoldierService soldierService;
#RequestMapping(value="/api/contextsource/soldier",method= RequestMethod.POST)
public Soldier addSoldier(#RequestBody Soldier soldier) {
return soldierService.addSoldier(soldier);
}
}
I was persisting the entity with in the wrong order. This is how I solved it.
#RequestMapping(value="/api/contextsource/soldier",method= RequestMethod.POST)
public Soldier addPeptide(#RequestBody Soldier soldier) {
for(Medal s: soldier.getMedals()) {
Medal ss = medalService.getMedalById(s.getId());
ss.getMedals().add(soldier);
medalService.addMedal(ss);
}
return null;
}
Did you try this in Soldier entity class like medal entity class:
#ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinTable(name = "soldier_medals", joinColumns = {
#JoinColumn(name = "soldier_id", nullable = false, updatable = false) },
inverseJoinColumns = { #JoinColumn(name = "medal_id",
nullable = false, updatable = false) })
private List<Medal> medals;
instead of this :
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "soldiers")
private List<Medal> medals;
I have Class Customer ,User , Customer has property manager of user class
Class Customer {
/** The manager. */
#ManyToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
#JoinColumn(name = "MANAGER")
#JsonSerialize(using = EntitySerializer.class)
#JsonDeserialize(using = UserDeserializer.class)
private User manager;
}
-------------------------------------
Class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = User.TABLE_NAME + "_SEQUENCE")
#SequenceGenerator(name = User.TABLE_NAME + "_SEQUENCE", sequenceName = User.TABLE_NAME + "_SEQ")
#Column(name = FIELD_ID, nullable = false)
#SuppressWarnings("PMD.ShortVariable")
private Integer id;
#Override
public Integer getId() {
return id;
}
#Override
public void setId(final Integer newId) {
//System.out.println("setID");
id = newId;
}
}
Now when i am trying to create criteria
final Criteria criteria = getSession().createCriteria(Customer.class);
criteria.add(Restrictions.ilike("manager", "%"+searchTerm+"%"))
It throwing Error :-
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.User.id
Caused by:
java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.User.id to java.lang.String
**Id field is integer **
Could you please change the following:
final Criteria criteria = getSession().createCriteria(Customer.class); criteria.add(Restrictions.ilike("manager", "%"+searchTerm+"%"))
by the following:
final Criteria criteria = getSession().createCriteria(Customer.class); criteria.add(Restrictions.ilike("manager.name", "%"+searchTerm+"%"))
LIKE clause is applicable to text column only.
this code to used
return this.sessionFactory.getCurrentSession()
.createCriteria(UserTraining.class)
.add(Restrictions.eq("userProfile.userId", userId))
.list();
You used this annotation to error remove
#Table(name="user_training")
#Entity
public class UserTraining {
#Id
#GeneratedValue
#Column(name="id")
private int id;
//Generate getter setter of id
/*
#Column(name="user_id")
private int userId;
*/
#JsonIgnore
#ManyToOne(cascade = {CascadeType.ALL})
#JoinColumn(name = "user_id")
private UserProfile userProfile;
public UserProfile getUserProfile() {
return userProfile;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
#OneToOne
#JoinColumn(name = "training_id")
private Training training;
#Column(name="view_count")
private int viewCount;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Training getTraining() {
return training;
}
public void setTraining(Training training) {
this.training = training;
}
public int getViewCount() {
return viewCount;
}
public void setViewCount(int viewCount) {
this.viewCount = viewCount;
}
}
I have two beans user and virtualdomain
#Entity
#Table(name = "tblUser")
public class User implements Serializable {
private Long id;
private String username;
private String deleteflag;
private Set<VirtualDomain> virtualdomainset;
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "username", length = 50, nullable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
#Column(name = "deleteflag")
public String getDeleteflag() {
return deleteflag;
}
public void setDeleteflag(String deleteflag) {
this.deleteflag = deleteflag;
}
#ManyToMany(targetEntity = VirtualDomain.class, cascade = {CascadeType.PERSIST},fetch=FetchType.EAGER)
#JoinTable(name = "tblUserDomainRel", joinColumns = #JoinColumn(name = "userid"), inverseJoinColumns = #JoinColumn(name = "domainid"))
public Set<VirtualDomain> getVirtualdomainset() {
return virtualdomainset;
}
public void setVirtualdomainset(Set<VirtualDomain> virtualdomainset) {
this.virtualdomainset = virtualdomainset;
}
}
#Entity
#Table(name = "tblVirtualDomain")
public class VirtualDomain {
private Long id;
private String domainname;
private String deleteflag;
private Set<User> userset;
#Id
#JoinColumn(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "domain_name")
public String getDomainname() {
return domainname;
}
public void setDomainname(String domainname) {
this.domainname = domainname;
}
#Column(name = "deleteflag")
public String getDeleteflag() {
return deleteflag;
}
public void setDeleteflag(String deleteflag) {
this.deleteflag = deleteflag;
}
#ManyToMany(cascade = {CascadeType.ALL},fetch=FetchType.EAGER, mappedBy = "virtualdomainset", targetEntity = User.class)
public Set<User> getUserset() {
return userset;
}
public void setUserset(Set<User> userset) {
this.userset = userset;
}
}
Now when I delete some user i use to set the deleteflag which means that the data remains in the database.
My requirement is that the user whose delete flag is set must be removed from the tblUserDomainRel table so how to write that delete query.
just remove the virtualDomain from the collection on the User and remove the other side for completeness
// on User
public void removeVirtualDomain(VirtualDomain vd){
virtualDomainset.remove(vd);
vd.getUserset().remove(this)
}
this will remove the relationship record. Or to remove a user from all virtual Domains:
// on User
public void removeFromAllVirtualDomains(){
for( VirtualDomain vd : virtualdomainset ){
vd.getUserset().remove(this);
}
virtualDomainset.clear();
}