How can I change a value of an id attribute to its equivalent description in DTO class using mapstruct - mapstruct

I have a 1 entity class PersonEntity and corresponding to that I have Dto class
#Entity(value="person")
class PersonEntity{
String name;
String id;
}
**DTO**
class PersonDto{
String name;
String id;
String desc; (This is a lookup from a map with id attribute)
}
In my mapper class, I am able to change my entity list to Dto list with below code.
public interface MyMapper{
List<PersonDto> entityListToDtoList(<List<PersonEntity>)
}
How can I use my lookup map to get the description and set in my DTO class. I am not able to figure out how to determine the value with below code.
List<PersonDto> entityListToDtoList(<List<PersonEntity>,#Context Map<String,String> lookupMap)

You have to define the PersonEntity to PersonDto method and add a #Mapping with an expression for that. See the following example:
#Mapping( target = "desc", expression = "java(lookupMap.get(person.getId()))" )
PersonDto entityToDto(PersonEntity person, #Context Map<String, String> lookupMap);
List<PersonDto> entityListToDtoList(List<PersonEntity> persons, #Context Map<String, String> lookupMap);

Related

How to populate values to Map of a POJO in MyBatis

I have a Java class that looks like this.
class Student {
private String studeID;
private String UUID;
private Map<String,Object> attributes;
}
And I have SQL to get students as follows.
SELECT studeID, UUID, F_NAME, L_NAME, PHONE FROM STUDENTS;
Need to populate F_NAME, L_NAME, PHONE to Map<String,Object> attributes of class Student.
How can I achieve this in iBatis/MyBatis XML.

Filtering a list in Objectify using Ref

How do i filter a list of an entity in Objectify which has Ref to another entity. The list should be filtered out based on a String field in the Ref entity.
public class AccountEntity extends BaseEntity {
#Index
private String accountName;
private String accountNo;
private String description;
private Integer displayOrderNo;
private Boolean contra = false;
private AccountingAccountType accountType;
#Index
private Ref<AccountGroupEntity> accountGroup;
#Ignore
private List<AccountEntryEntity> accountLedgerEntries;
public AccountEntity() {
}
this is ref entity
filter code
A ref is a key, so you can filter the key by passing in either a Key, Key<>, Ref<> or #Entity pojo.
You cannot however filter on a property of the entity that the key points to. To do this you'll need to denormalise that property into a separate indexed list in this entity, or create a lookup entity, similar to a join table.

spring data jpa fine granular auditing, custom audit

I have requirement where I need to insert user name and group name to which the user belongs (both available in SecurityContext) in the same table.
class Entity
{
#createdBy
String username
#createdBy
String groupname
other fields ...
}
As per requirement. I cant solve this issue by making a user class and referencing it through a foreign key.
With current implementation of AuditingHandler both fields are getting the same value. How do I make sure they get respective values.
Can this be achieved using current implementation ?
If not thn how can I provide custom implementation of AuditingHandler ?
You could make a separate embeddable class and annotate it with #CreatedBy in your parent class. One way is to define a bean implementing AuditorAware, then you can make it return custom object, containing your two required fields. For example, your parent class would look like this (note the listener annotation):
#Entity
#EntityListeners(AuditingEntityListener.class)
public class AuditedEntity {
#Id
#GeneratedValue(generator = "uuid")
#GenericGenerator(name = "uuid", strategy = "uuid")
private String id;
#Embedded
#CreatedBy
private AuditorDetails createdBy;
// setters and getters
}
where AuditorDetails is:
#Embeddable
public class AuditorDetails {
private String username;
private String groupname;
// setters and getters
}
and finally, your AuditorAware bean:
#Component
class AuditorAwareImpl implements AuditorAware<AuditorDetails> {
#Override
public AuditorDetails getCurrentAuditor() {
return new AuditorDetails()
.setUsername("someUser")
.setGroupname("someGroup");
}
}
AuditingHandler fetches your custom AuditorDetails from your AuditorAware bean (it must be single bean implementing it) and sets it in your auditable entity.

Cannot use an #IdClass attribute for a #ManyToOne relationship

I have a Gfh_i18n entity, with a composite key (#IdClass):
#Entity #IdClass(es.caib.gesma.petcom.data.entity.id.Gfh_i18n_id.class)
public class Gfh_i18n implements Serializable {
#Id #Column(length=10, nullable = false)
private String localeId = null;
#Id <-- This is the attribute causing issues
private Gfh gfh = null;
....
}
And the id class
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private Gfh gfh = null;
...
}
As this is written, this works. The issue is that I also have a Gfh class which will have a #OneToMany relationship to Gfh_i18n:
#OneToMany(mappedBy="gfh")
#MapKey(name="localeId")
private Map<String, Gfh_i18n> descriptions = null;
Using Eclipse Dali, this gives me the following error:
In attribute 'descriptions', the "mapped by" attribute 'gfh' has an invalid mapping type for this relationship.
If I just try to do, in Gfh_1i8n
#Id #ManyToOne
private Gfh gfh = null;
it solves the previous error but gives one in Gfh_i18n, stating that
The attribute matching the ID class attribute gfh does not have the correct type es.caib.gesma.petcom.data.entity.Gfh
This question is similar to mine, but I do not fully understand why I should be using #EmbeddedId (or if there is some way to use #IdClass with #ManyToOne).
I am using JPA 2.0 over Hibernate (JBoss 6.1)
Any ideas? Thanks in advance.
You are dealing with a "derived identity" (described in the JPA 2.0 spec, section 2.4.1).
You need to change your ID class so the field corresponding to the "parent" entity field in the "child" entity (in your case gfh) has a type that corresponds to either the "parent" entity's single #Id field (e.g. String) or, if the "parent" entity uses an IdClass, the IdClass (e.g. Gfh_id).
In Gfh_1i8n, you should declare gfh like this:
#Id #ManyToOne
private Gfh gfh = null;
Assuming GFH has a single #Id field of type String, your ID class should look like this:
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private String gfh = null;
...
}

Can i user instance method in JPA entity?

public class Dept{
private String id;
private String name;
private String address;
private List<Student> students;
public static List <Student> getStudentByDeptid(EntityManager em, Dept dept)
{
..............
}
}
getStudentByDeptid() should be static or non static?
Yes you can. But you shouldn't JPA entity are not designed to have business logic in them you should use Session Bans for the same. You can define a StudentBean as session bean and define this logic in that bean.
Assuming you are calling query on em passed to function.