I'm trying to filter a result set by a foreign key:
createCriteria(Person.class).add(Restrictions.ne("position", 1L)).list()
But getting this exception: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.example.model.Position.id
Here are the necessary JPA entities (trimmed down to the necessary fields):
#Entity
#Table
public class Person {
#Id
#GeneratedValue
private Long id;
#ManyToOne
#JoinColumn(nullable = false)
#ForeignKey(name = "person_position_fkey")
private Position position;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}
#Entity
#Table
public class Position {
#Id
#GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Try Restrictions.ne("position.id", 1L)
Related
I am wondering if I could communicate between entities in different backend projects in intelliJ idea (Maven/Springboot).
I have a project in which the entity 'Pakketje' can be found. And I also have a project in which the entity 'Bedrijf' can be found.
I would like to have a #oneToMany relationship between 'Bedrijf' entity and 'Pakketje' entity.
So one 'Bedrijf' has a List of 'Pakketjes'.
This is the entity from project number 1, 'Pakketje':
#Entity
public class Pakketje {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int code;
private String status = "In magazijn";
public Pakketje() {
}
public Pakketje(int Id, int Code) {
this.id = Id;
this.code = Code;
this.status = "In magazijn";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
This is the entity from project number 2, 'Bedrijf':
#Entity
public class Bedrijf {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String bedrijfsnaam;
private String gebruikersnaam;
private String wachtwoord;
private List<Pakketje> pakketjes;
public Bedrijf() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBedrijfsnaam() {
return bedrijfsnaam;
}
public void setBedrijfsnaam(String bedrijfsnaam) {
this.bedrijfsnaam = bedrijfsnaam;
}
public String getGebruikersnaam() {
return gebruikersnaam;
}
public void setGebruikersnaam(String gebruikersnaam) {
this.gebruikersnaam = gebruikersnaam;
}
public String getWachtwoord() {
return wachtwoord;
}
public void setWachtwoord(String wachtwoord) {
this.wachtwoord = wachtwoord;
}
}
Unfortunately the 'Bedrijf' entity does not recognize the 'Pakketje' entity because they are in 2 separate projects.
Can this be resolved? or what could be an alternative? School tells me I MUST have 2 backend projects communicating with each other.
I'm experimenting with mapstruct and follow this tutorial:
mapstruct tut
I have this entity:
#Entity
public class Company {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_company")
#SequenceGenerator(name = "seq_company", allocationSize = 1)
#Column(nullable = false, updatable = false)
private Long id;
private String name;
private String shortName;
public Company() {
}
public Company(Long id, String name, String shortName) {
this.id = id;
this.name = name;
this.shortName = shortName;
}
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;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
}
And this is the simple dto:
public class CompanyDto {
#JsonProperty("id")
private Long id;
#JsonProperty("name")
private String name;
#JsonProperty("shortName")
private String shortName;
}
And here is the mapper interface:
#Mapper(componentModel = "spring")
public interface CompanyMapper {
CompanyDto companyToCompanyDto(Company company);
Company companyDtoToCompany(CompanyDto companyDto);
List<CompanyDto> companiesToCompanyDtos(List<Company> companies);
}
I certanly oversee something, because there is no setters in the generated implementation, f. e.:
#Override
public Company companyDtoToCompany(CompanyDto companyDto) {
if ( companyDto == null ) {
return null;
}
Company company = new Company();
return company;
}
What goes here wrong?
I've noticed that your CompanyDto class has private fields but no getters or setters. There is no standard way to access the fields in that class. You might need to add those in order to map in or out of that class.
I have this code (just multiply the number of attributes by ALOT):
#Entity
#Table(name = "MY_TABLE")
#Immutable
public class MyEntity {
#Id
#Column(name = "ID")
private String id;
public static final String PROPERTYNAME_ID = "id";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
/* used by JPA, needs to have same getters/setters as MyEntity */
public class MyEntityCriteria {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Is there a way to share the attributes, getters and setters between those class?? ('extends' doesn't work as it also shares annotations...)
I have two entities and one with a field of a specific enum. Now I want to retrieve all that have a specific enum value.
#Entity
#Table(name = "AlarmEvent")
#XmlRootElement
public class AlarmEvent implements Identifiable {
#Id
#GeneratedValue(generator = "increment")
#GenericGenerator(name = "increment", strategy = "guid")
#Column(name = "Id", columnDefinition = "uniqueidentifier")
private String id;
#ManyToOne
#JoinColumn(name = "AlarmType_Id")
#XmlJavaTypeAdapter(EntityAdapter.class)
private AlarmEventDefinition alarmType;
#Column(name = "Timestamp", nullable = false)
private Date timestamp;
#Override
#XmlTransient
public String getIdentifier() {
return id;
}
public AlarmEventDefinition getAlarmType() {
return alarmType;
}
public void setAlarmType(AlarmEventDefinition alarmType) {
this.alarmType = alarmType;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
and the second entity
#Entity
#Table(name = "AlarmEventDefinition")
#XmlRootElement
public class AlarmEventDefinition implements Identifiable {
public static enum AlarmEventDefinitionType {
Start, End, Trigger, Report
}
#Id
#Column(name = "Id")
private Integer id;
#Column(name = "Name_DE", length = 256)
private String nameDe;
#Column(name = "Type")
#Enumerated(EnumType.STRING)
private AlarmEventDefinitionType type;
#OneToMany(mappedBy = "alarmType", fetch = FetchType.LAZY)
#XmlTransient
private List<AlarmEvent> events;
public List<AlarmEvent> getEvents() {
return events;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNameDe() {
return nameDe;
}
public void setNameDe(String nameDe) {
this.nameDe = nameDe;
}
public AlarmEventDefinitionType getType() {
return type;
}
public void setType(AlarmEventDefinitionType type) {
this.type = type;
}
}
Now I want to do this
List<AlarmEvent> result = (List<AlarmEvent>) session.createCriteria(AlarmEvent.class)
.add(Restrictions.eq("alarmType.type", AlarmEventDefinitionType.Trigger))
.list();
To retrieve all AlarmEvents thiers AlarmEventDefinitions type is Trigger.
Hibernate says:
org.hibernate.QueryException: could not resolve property:
alarmType.type of: datamodel.AlarmEvent
You just have an alarmType property defined in the AlarmEvent class, there is no alarmType.type. That's the error message is saying.
You need to extend the criteria to the associated class AlarmEventDefinition. The code should be something like this:
List<AlarmEvent> result = (List<AlarmEvent>) session.createCriteria(AlarmEvent.class)
.createCriteria("alarmType").add(Restrictions.eq("type", AlarmEventDefinitionType.Trigger))
.list();
I have a Hibernate mapping that looks something like this:
class A {
private Long id;
private Map<C,String> someMap;
...
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#ElementCollection(targetClass=String.class, fetch=FetchType.EAGER)
public Map<C, String> getSomeMap() {
return someMap;
}
pubic void setSomeMap(Map<C,String> someMap){
this.someMap = someMap;
}
}
class B {
private Long id;
private A a;
...
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#OneToOne
public A getA(){
return a;
}
public void setA(A a){
this.a = a;
}
}
class C {
private Long id;
...
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
How can I do a query (with criteria) to get all "В" which have such "A" which have, for example, "test" as value in "someMap"? Or can I do it only with clear sql?
Interesting. I've never done a map where key is the object, and string the value. Generally it's the other way around. Can you keep the string on C, and use a straight collection? Or is there a funky many to many relationship going on here?