naming parameters for ORMLITE - ormlite

I have looked through the ORMLITE Documentation for naming.
Let's say I have an object called Contract. When I call the API for Contract, it contains two parameters: name & type. When I create a class for Contract, do I have to match the two parameters with that from the API call, so that it regconise those parameters?
i.e.
public class Contract {
#Key
#DatabaeField(columnName = BaseColumns._ID, id = true)
public int id;
#Key
#DatabaseField
public String name;
#Key
#DatabaseField
public String type;
public Contract() {}
}
Thx for your help.

Related

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.

How to adapt a child node in sling model of aem6

I am learning to use one of the new features of AEM6 - Sling Models. I have already fetched the properties of a node following the steps described here
#Model(adaptables = Resource.class)
public class UserInfo {
#Inject #Named("jcr:title")
private String title;
#Inject #Default(values = "xyz")
private String firstName;
#Inject #Default(values = "xyz")
private String lastName;
#Inject #Default(values = "xyz")
private String city;
#Inject #Default(values = "aem")
private String technology;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getTechnology() {
return technology;
}
public String getTitle() {
return title;
}
}
and adapted it from a resource
UserInfo userInfo = resource.adaptTo(UserInfo.class);
Now i have the hierarchy as -
+ UserInfo (firstName, lastName, technology)
|
+ UserAddress (houseNo, locality, city, state)
Now I want to fetch the properties of the UserAddress.
I had got some hints from the documentation page, such as -
If the injected object does not match the desired type and the object implements the Adaptable interface, Sling Models will try to adapt it. This provides the ability to create rich object graphs. For example:
#Model(adaptables = Resource.class)
public interface MyModel {
#Inject
ImageModel getImage();
}
#Model(adaptables = Resource.class)
public interface ImageModel {
#Inject
String getPath();
}
When a resource is adapted to MyModel, a child resource named image is automatically adapted to an instance of ImageModel.
but I don't know how to implement it in my own classes. Please help me out with this.
It sounds like you need a separate class for the UserAddress to wrap the houseNo, city, state and locality properties.
+ UserInfo (firstName, lastName, technology)
|
+ UserAddress (houseNo, locality, city, state)
Just mirror the structure you outlined in your Sling Models.
Create the UserAddress model:
#Model(adaptables = Resource.class)
public class UserAddress {
#Inject
private String houseNo;
#Inject
private String locality;
#Inject
private String city;
#Inject
private String state;
//getters
}
This model can then be used in your UserInfo class:
#Model(adaptables = Resource.class)
public class UserInfo {
/*
* This assumes the hierarchy you described is
* mirrored in the content structure.
* The resource you're adapting to UserInfo
* is expected to have a child resource named
* userAddress. The #Named annotation should
* also work here if you need it for some reason.
*/
#Inject
#Optional
private UserAddress userAddress;
public UserAddress getUserAddress() {
return this.userAddress;
}
//simple properties (Strings and built-in types) omitted for brevity
}
You can tweak the behaviour with additional annotations for default values and optional fields but this is the general idea.
In general, Sling Models should be able to handle an injection of another model as long as it finds an appropriate adaptable. In this case, it's another Sling Model but I've done it with legacy classes based on adapter factories as well.

querydsl instance variables

From QueryDSL JPA Tutorial, I could not find differences between default instance variable generated by querydsl and custom variable.
For the entity Customer defined as
#Entity
public class Customer {
private String firstName;
private String lastName;
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public void setFirstName(String fn){
firstName = fn;
}
public void setLastName(String ln)[
lastName = ln;
}
}
What is the difference between using default instance variable and custom as follows ?
QCustomer customer = QCustomer.customer;
VS
QCustomer customer = new QCustomer("myCustomer");
What could be the possible use cases for custom variable as in second one?
The variable name is used as such in the serialization. If you need to refer to multiple instances of the same type in your query, you need to use multiple variables.
Here is an example
QCustomer customer = QCustomer.customer;
QCustomer customer2 = new QCustomer("customer2");
List<Customer> customers = query.from(customer)
.where(new JPASubQuery()
.from(customer2)
.where(customer2.id.ne(customer.id),
customer2.lastName.eq(customer.lastName),
customer2.firstName.eq(customer.firstName))
.exists())
.list(customer);

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.