JPA and JSF Datatable Optimization - jsf

I need to display a large table with about 1300 roles at one time. (I know I should use a data scroll but my users want to see the whole table at one time.) The table displays 4 columns. Two of those columns are from the object but the other two are from referenced objects in the original object. I need to find the best/efficient way to do this. I currently have this working but when I reload the table it give an out of memory error. I think that it's caused by the large amount of redundant data in memory.
Create a view object that the repository will fill in only the needed fields.
Any other suggestions.
Here are the objects:
public class Database extends EntityObject {
private Long id;
private String name;
private String connectionString;
private String username;
private String password;
private String description;
// getter and setters omitted
}
public class Application extends EntityObject {
private Long id;
private String name;
private String fullName = "";
private String description;
private Database database;
private List<Role> roles = new ArrayList<Role>(0);
// getter and setters omitted
}
public class Role extends EntityObject {
private Long id;
private String name;
private String nameOnDatabase;
private Application application;
// getter and setters omitted
}
What I need displayed from the list of Roles is:
role.id, role.name, role.application.name, role.application.database.name

To optimize wisely, define what are you going to do with data, view or/and edit. Here are some common scenarios:
Retrieval using lazy fetch type.
Mark your roles in application with FetchType.LAZY annotation.
Retrieval using multiselect query. Create your custom class (like DTO) and populate it with data from the database using multiselect query. (Similar to VIEW mapped as Entity)
There are also other possibilities, such as Shared (L2) Entity Cache or Retrieval by Refresh.
See if you are using EntityManager correctly reading Am I supposed to call EntityManager.clear() often to avoid memory leaks?.

Related

Handling aggregate root

I'm new in DDD so I'm doing some practice to undertand a little bit more. I have Course BC with the follow rules:
Course has to be created first and then they can create the modules of one course
Every module will be finished by the user when he upload the homework
The course will be finished by the user when he finished all the modules
Definition:
A course covers a particular topic and it is comprised of module. For instance, sap course has 10 modules such as: module 1: what is it?, module 2: how to use it?…
After this, I realize that Course is the aggregate root of module, because the modules are finished I have to close the status of user with the course.
the model would be:
public class Course : AggregateRoot
{
private string title;
private List<Module> modules;
}
but also module is an aggregate root of homework because when the user upload his homework the module has to be closed. This make me think that this approach is wrong because is not possible in DDD have nested aggregate root. Someone knows what is it wrong?
[UPDATED]
Ok, now I understand how is work and why you split it in 2 BC. However I did some changes and some questions come to my mind.
-I've created enroll method as static and I put the constructor as private.
-Course have to be an array because one student can have more than one.
-I've put more parameters related with the course and also the teacher. Is the teacher and entity of course, right?
-I created status of course to update it when the module is finished this way I don't have to read all the modules to know it. is ok?
-How can I pass more information for every module like title and description? and is the course entity how create all the modules, right?
public class StudentEnrolment: AggregateRoot
{
private StudentId studentId;
private Course courses;
private constructor(
StudentId studentId,
Course course,
){
this.studentId= studentId;
this.courses[] = course;
}
public statuc function enroll(
StudentId studentId,
CourseId courseId,
string courseTitle,
string courseLink,
string teacherId,
string teacherName,
List<Tuple<ModuleId, string>> modules) {
teacher = new Teacher(...);
courseStatus = new courseStatus();
new course(courseTitle, courseLink, courseStatus, teacher);
return new self(studentId, course);
}
public function void uploadModuleHomework(ModuleId moduleId, Homework homework){
/* forward to course.uploadModuleHomework */
}
public boolean isCourseFinished(){
/* forward to course.isFinished */
}
public List<Tuple<ModuleId, string>> getModules(){
/* forward to course.getModules */
}
}
There are two different sub-domains (so we have two bounded contexts):
1.Courses and modules administration where the teachers can administer those; Here Course and Module can be Aggregate roots and a course could hold references to the Modules IDs (not to instances!).
public class Course: AggregateRoot
{
private string title;
private List<ModuleId> modules;
}
2.Student participations to the courses. Here there is a StudentEnrolment Aggregate root that contains references to the Course and Module from the other BC but as Value objects; it models the student participation to a single course; in this bounded context there is a new Entity, Homework, that track the student homework-upload and course participation status.
public class StudentEnrolment: AggregateRoot
{
private StudentId studentId;
private Course course;
private List<Homework> homeworks;
// initialize a student enrolment as public constructor or make constructor private and use a static method
// here is important to notice that only this AR creates its entities, it does not receive them as parameter
public constructor(
StudentId studentId,
Course course,
List<Module> modules){
this.studentId = studentId;
this.course = course;
//build the the homeworks entity list based on the modules parameter
//for each module create a Homework entity, that initially is not uploaded, like:
this.homeworks = modules.map(module => new Homework(module))
}
public function void uploadFileForHomework(ModuleId moduleId, string file){
/* find the homework by module Id and upload file*/
}
public boolean isCourseFinished(){
/*returns true if all homeworks are uploaded*/
/*optimization: you could have a status that is updated when a homework's file is uploaded*/
}
public List<Tuple<ModuleId, string, boolean>> getHomeworks(){
/* returns a list of readonly Homeworks, i.e. Tuple<ModuleId, string /*module title*/, boolean /*is uploaded*/> */
}
}
public class Homework: Entity
{
private Module module;
private string file;
public constructor(Module module){
this.module = module;
}
public void upload(string file){ this.file = file;}
public boolean isUploaded(){return (boolean)this.file;}
public string getUploadedFile(){return this.file;}
public ModuleId getModuleId(){return this.module.getId();}
}
public class Course: ValueObject
{
private string title;
private CourseId id;
public constructor(id, title){...}
public string getTitle(){return this.title;}
public string getId(){return this.title;}
}
public class Module: ValueObject
{
private string title;
private string description;
private ModuleId id;
public constructor(id, title, description){...}
public string getTitle(){return this.title;}
public string getDescription(){return this.description;}
public string getId(){return this.title;}
}
If you need to query the Enrolment to get the homeworks you should not return a list of Homeworks because the client code would think that it can call Homework.upload(file) directly, which is not permitted (only the Aggregate root can modify its internal entities). Instead, you could return a Tuple or better, you can create an immutable version of the Homework class.

How to use UserDefinedType(UDT) with Spring Data Cassandra for List<UDT>

I have created a UDT named widgetData in cql for which i have a corresponding POJO class named widgetData. I want to use this in another domain POJO class as List. What kind of annotation should be used to do so?
#Table("dashboardManagement")
public class Dashboard implements Serializable {
#Column("dashboardState")
#CassandraType(type = DataType.Name.UDT, userTypeName = "widgetData")
private List<widgetData> dashboardState;
....
The above code does not work.
Do I have to write a seperate userTypeResolver for this?
I realize this question is a little old, but I have made this work.
Basically, I had a user profile with an address UDT, and that UDT had its own POJOs/entity classes. The UDT address entity class used the #UserDefinedType annotation:
#UserDefinedType("address")
public class AddressEntity implements Serializable {
private static final long serialVersionUID = 1817053316281666003L;
#Column("mailto_name")
private String mailtoName;
private String street;
private String street2;
private String city;
...
The user entity utilized the Address UDT entity:
#Table("user")
public class UserEntity implements Serializable {
private static final long serialVersionUID = 4067531918643498429L;
#PrimaryKey("user_id")
private UUID userId;
#Column("user_email")
private String userEmail;
#Column("first_name")
private String firstName;
#Column("last_name")
private String lastName;
#Column("addresses")
private List<AddressEntity> addresses;
...
Then, it was a simple matter to map a user's address data to a UserEntity object (userE below) and save it with standard repository methods.
// save to DB
userRepo.save(userE);
You can find everything built to support the User services here: https://github.com/datastaxdevs/workshop-ecommerce-app/blob/main/backend/src/main/java/com/datastax/tutorials/service/user/
So I would say to have a look at the class for the widgetData object, make sure it's using the #UserDefinedType annotation, and mark the column using the #Column annotation in the Dashboard class (basically, get rid of the #CassandraType):
#Column("dashboardState")
private List<WidgetData> dashboardState;

JPQL query help: many-to-many relationship filtered by a collection

I'm having trouble writing a query using JPQL. I have two entities with a many-to-many relationship:
Entity1 (User)
#Entity
public class User implements Serializable {
#Id
private long id;
private String name;
#ManyToMany()
#JoinTable(name="users_roles",
joinColumns=#JoinColumn(name="user_id"),
inverseJoinColumns=#JoinColumn(name="role_id"))
private Set<Role> roles;
}
Entity2 (Role)
#Entity
public class Role implements Serializable {
#Id
private long id;
private String name;
}
Imagine that I have a list of roles that can vary in size (e.g. "roleA", "roleB", "roleC", ...), and I want to get every user that has every role in this list. In my head, it would be something like this:
select u from users u where :roles member of u.roles
As far as I know this doesn't work because :roles can't be a collection when used at the left hand side of "member of". Is there a way to achieve this in one JPQL query? If it is not possible in a single query, then what would be the best approach?
Thanks
Try this:
select u FROM User u JOIN u.roles r WHERE r IN (:roles)

Xamarin.ios initialize UIView

I am using Xamarin.iOS. I have created UIView with a few UITextFields. I am looking for best way to initialize text value in these textfields from code.
I can pass text data in the constructor of UIViewContoller, but I don't have access to textFields inside it (they are null). I can change text value of textFields in viewDidLoad method.
I don't want to create additional fields in controller class to store data passed by constructor and use them in viewDidLoad. Do you know better solution ?
I don't want to create additional fields in controller class to store
data passed by constructor and use them in viewDidLoad.
But that's how it's meant to be done.
Alternatively, you can create less fields/properties in your viewcontroller if you use a MVVM pattern:
public class UserViewModel {
public string Name { get; set;}
public string Title { get; set;}
}
public class UserViewController : UIViewController
{
UserViewModel viewModel;
public UserViewController (UserViewModel viewModel) : base (...)
{
this.viewModel = viewModel;
}
public override void ViewDidLoad ()
{
userName.Text = viewModel.Name;
userTitle.Text = viewModel.Title;
}
}
That's the kind of pattern which gives you a lot of code reuse accross platforms (android, WP, ...) and clearly separate concerns. It's a (very) little bit of extra code, but it's worth every byte.

What approach is good for US State ListBox/DropDownList

What is the best approach to bind US State in WPF (in ListBox or in DropDownList)? Should I use DataTable to bind this data? Is binding DataTable to WPF object right programming approach? Or Should I use class/object. I mean get data from database and convert it to Generic Object List and then bind this list to WPF object?
Thanks,
public class States
{
private string name;
public string Name
{
get
{
return name;
}
}
private string id;
public string Id
{
get
{
return id;
}
}
}
List<States> states = new List<States>();
//get from database
foreach( states DataSource)
{
name = "Alabama";
id = "1";
}
// next Cache list of states for better performance
Many ways...
One approach is to use a list class. get from data source, next cache for better performance.

Resources