Repeated Records in Java - java

Is there any way to create this type of structure in Java?
In Python it is possible, but we're programming the App Engine in Java and we don't have any idea how to make this.

Just use a List:
#Cache
#Entity
class MyClass implements IsSerializable{
public List<Log> logs;
...
In your Log class define your 4 fields:
#Entity
public class Log implements IsSerializable {
public String property1;
public String property2;
public String property3;
public String property4;
....
}
I assume you are using Objectify

Related

How to search in h2 database Play Framework 2.7.3?

I need to search in an h2 database for Company class instances that have a specific code, but I can't figure out how.
I have tried using the Finder class, but there don't seem to be any find methods in the version i am using except findbyid().
Here is the beginning of my Company class:
#Entity
public class Company extends Model {
#Id
public Integer id;
public String code;
public String name;
public String adress;
public String fiscalCode;
public String bankAccount;
public static Finder<Integer, Company> find = new Finder<>(Company.class);
Thank you!
Depending on the version of Play you are using I'd suggest to try the following:
Company.find.where().like("code", "%foo%").findList();
or
Define your finder
public class CompanyFinder extends Finder<Long,Company> {
public CompanyFinder() {
super(Company.class);
}
// Add finder methods ...
public Company byCode(String code) {
return query().eq("code", code).findOne();
}
}
Update your entity to reference this finder:
#Entity
public class Company extends BaseModel {
public static final CompanyFinder find = new CompanyFinder();
...
}
Call the finder:
Company c = Company.find.byCode("foo");

Spring Data JPA mapping nested entities

I'm a little bit confused about using projections in Spring Data JPA.
I wanted to optimize my queries by requesting only needed columns (preferably) in one query, and I thought that using projections is a good idea. But it seems that projection with nested projection becomes open and requests all columns and further nesting is impossible.
I've tried to find a solution with #Query (cannot find how to map nested lists), #EntityGraph (cannot find how to request only specified column) and #SqlResultSetMapping (cannot find how to make mapping nested lists), but it hasn't worked for me.
Is there any solution except receiving List<Object[]> and manually mapping?
I have the next entities classes (simplified for the question):
public class TestAttempt{
private Long id;
private User targetUser;
private Test test;
}
public class Test{
private Long id;
private String name;
private Set<Question> questions;
}
public class Question{
private Long id;
private String name;
private Test test;
}
And I wanted to write something like this (it can be just TestAttempt with null in unused fields):
public interface TestAttemptList {
Long getId();
Test getTest();
interface Test {
String getName();
List<Question> getQuestions();
interface Question {
String getName();
}
}
}
public interface TestAttemptRepository extends JpaRepository<TestAttempt, Long> {
List<TestAttemptList> getAllByTargetUserId(Long targetUserId);
}
And in result get something like this:
{
id: 1,
test: {
name: test1,
questions: [{
name: quest1
}, {
name: quest2
}]
}
}
Ive done something like this... You'll have your repository interfaces which will extend CrudRepository et. al. with the full objects (TestAttempt etc) You define your projections separately. The projection interfaces can contain other projection interfaces (TestAttemptSummary can contain a TestSummary) When the projection interface is used within the given repository the defined methods are applied to the object type the repository is configured for. Something like this.
public interface TestAttemptSummary {
Long getId();
TestSummary getTest();
}
public interface TestSummary {
String getName();
List<QuestionSummary> getQuestions();
}
public interface QuestionSummary {
String getName();
}
public interface TestAttemptRepository extends CrudRepository<TestAttempt, Long> {
TestAttemptSummary getTestAttemptSummary();
}

How to ceate my own user type using jadira usertype

I have my class MyClass that can be seralized/deserialized to/from string.
I looking for a simplest way use it as a hibernate property and serialize using jadira.
class MyClass {
#ToString
public String toString() {}
#fromString
public static MyClass fromString(String encoded) {}
}
And then, use it in an entity
#Entity
class MyEntity {
#Type(type="org.jadira.usertype....")
private MyClass field;
}
My question is what should I write inside #Type?
Thank you.
Try something like:
#TypeDef(name="BoundType", typeClass="org.jadira.usertype.bindings.PersistentBoundClass")
....
#Type(type = "BoundType", parameters = { #Parameter(name="javaClass", value="MyClass"), #Parameter(name="hibernateClass", value="java.lang.String.class")})

How to read a object from mongodb by spring-data-mongodb polymorphically,from 1.0.4 up to 1.3.1

I have one abstract class and one concrete class.
abstract class FileModel{
public abstract void setName();
public abstract String getName();
}
class Container implements FileModel{
private String name;
#override
public void setName(String name){this.name=name;}
#override
public String getName(){return this.name;}
}
and my find method was like this:
public interface ContainerRepository extends
CrudRepository<Container, String>,
MongoRepository<Container, String>,
PagingAndSortingRepository<Container, String> {
#Query(value = "{ 'pid' : ?0,'status':{'$ne':'deletion'}}", fields = "{'name' : 1,'referenceId' : 1,'_class':1}")
List<FileModel> findByPid(String pid, Pageable pageable);
}
After I run the method findByPid,I got the following error:No property name found on FileModel . Is it necessary to add that property name in the abstract class? When back to spring-data-mongodb 1.0.4, all that code above worked fine. Now I just upgraded to 1.3.1, it broke. Anything that can workaround? Thanks.
After searching on st and some googling. I found some similar questions and did some coding,turns out all I need to do is moving all the private fields from the concrete class into the abstract class and make them protected except the #id idfield.Then all things on the go.

How can I use find() or filter() using Play-Morphia on a hash-map object?

I'm using Play framework and Play-Morphia for using MongoDb in Java.
I got an entity Message which defined as follows:
#Entity
public class Message{
public String senderId;
public Date date;
public String initiatorId;
#Embedded public MessageBasicInfo basicInfo;
}
The MessageBasicInfo defined as follows:
public class MessageBasicInfo implements IModel{
public HashMap<String, Boolean> recipients;
}
Now, I want to retrieve from Mongo only the messages which their value in recipients' hashmap is true.
I know how to do it in list for example but no idea how to do it with a hashmap object.
I'll appreciate any suggestions.
Thanks.
HashMap is very hard to query. I recommend you re-organize your MessageBasicInfo as
public class MessageBasicInfo implements IModel {
public String key;
public Boolean value;
}
And then you can query for all true value messages via:
Message.q("basicInfo.value", true).asList();

Categories

Resources