How can I get data from two tables? - postgresql

I am using Hibernate and PostgreSql. In hibernate I have two classes like:
Mobile(id, name, serial no,model_no)
Model(id, description, value)
Now Model class's values are stored in Mobile class (model_no). This Mobile class doesn't have any reference to Model class.
In Model class has data like:
Modal
======
id description value
1 aaaa 1
2 bbbbb 2
3 ccccc 3
4 ddddd 12
5 eeee 40
Here this value is being stored in Mobile table. In Mobile table I have model_no as 0 (which is not in Model, I don't want to put that value in Model table, because if I put I needs to change lot of code.
Now what I want a query to get the out put like
value description
---- ------------
0 UNKNOWN
1 aaaa
2 bbbbb
3 ccccc
4 ddddd
5 eeee.
like this. To get the all model_nos I can use a query like
select modal_no from Mobile
where modal_no in(select value from Modal) or model_no = 0
but here I what the description also which is in Model table. Can anybody help?
Thank u bro for your response, as i mentioned this Mobile table(Hibernate Mobile class) don't have reference to the Model table(in hibernate Model class). If i have reference to Model in Mobile class then your answer will be 100% correct. In my Mobile class this model_no is integer value. If i use your query in hql i will get the exception like "path expected". I want Hql(or sql) query to get the output with 0 value.
my hibernate Mobile class is like this
class Mobile {
int id;
int model_no; // this model_no has 0 + modal_no values(1,2,3,12,42).
// some references like
Manufacture manf;
SerialNo serialno;
Customer cust;
getters and setters
}
My Hibernate Model class is like ...
class Model{
int id;
String description;
int value; this column has 1,2,3,12,42. it don't have 0.
setters and getters.
}

A left join would accomplish that:
select Mobile.modal_no
, Modal.description
from Mobile
left join
Modal
on Mobile.model_no = Modal.value

Related

Adding elements on many-to-many relation

I am making a project where (from the perspective of school) you can calculate each student average.
You can register a student (first entity) on a screen and subjects (second entity) on another screen.
Student has name, email, grade and average as atributes, and Subjects has name. They relate many-to-many with each other.
I am trying to create a copy of subjects list to each student, then on each student i can register a grade to each subject. Like this:
Model concept
Model:
!https://imgur.com/gmXyR5j
I've created a singleton of subjects since it is used more than one location:
import Foundation
import CoreData
class SubjectsManager {
static let shared = SubjectsManager()
var subjects: [Subject] = []
func loadSubject(with context: NSManagedObjectContext) {
let fetchRequest: NSFetchRequest<Subject> = Subject.fetchRequest()
let sortDescritor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescritor]
do {
subjects = try context.fetch(fetchRequest)
} catch {
print(error.localizedDescription)
}
}
func deleteSubject(index: Int, context: NSManagedObjectContext) {
let subject = subjects[index]
context.delete(subject)
do {
try context.save()
subjects.remove(at: index)
} catch {
print(error.localizedDescription)
}
}
private init() {
}
}
And, on my student screen, i've tried many thing but nothing is working.
The to-many relation of student with subject is called registeredSubjects
I've created a NSSET called subjectsManagerSet to get values from the singleton, but it not working. Here what i've tried so far:
subjectManagerSet.addingObjects(from: subjectsManager.subjects)
Also tried to create a for loop of subjectManager.subjects to add on subjectManagerSet but it's not working too.
About errors, when i get samples from xcode output, it keep showing that subjectManagerSet did not get values from subjectManager.subject
Error message:
2019-09-26 20:38:16.983725-0300 MyAverage[1734:62290] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/vitorgomes/Desktop/Mentorizacao/MyAverage/MyAverage/Controllers/StudentsViewController.swift, line 119
(lldb) po subjectManagerSet
0 elements
The expected result is that i want a copy of subjects for each student instance, then i can add grades for each subjects for each student.
The expected result is that i want a copy of subjects for each student
instance, then i can add grades for each subjects for each student.
I am not addressing your problem as stated at the beginning of your question, but the one stated at the end, per the quote above.
I would encourage you to reconsider the structure of your model.
Perhaps something like this?
In this proposed model, you're able to assign to an object of the entity Enrolment:
a grade (and date) via these attribute properties;
a student via the one-to-many relationship property with a Student entity;
a subject via the one-to-many relationship property with a Subject entity.
In the following examples, I assume Core Data generated NSManagedObject subclasses - that is - in the Data Model Inspector, set the value for Codegen = Class Definition (default).
(Personally and as an aside, I prefer to manually write NSManagedObject subclasses for each of my entities and use Set rather than the NSSet, as I find it subsequently a lot easier to maintain type integrity in my code. But I have not done that here as most people who are new to Core Data will use the default for Codegen noted above.)
You're able to access these values in the following manner...
let student = Student()
print("Student name is: \(String(describing: student.name))")
if let studentEnrolments: NSSet = student.studentEnrolments {
for item in studentEnrolments {
if
let enrolment = item as? Enrolment,
let subjectName: String = enrolment.subject?.name {
print("Subject name for this student is: \(subjectName)")
}
}
}
Its easy to assign a subject enrolment to a student...
let enrolment = Enrolment()
let subject = Subject()
enrolment.subject = subject
student.addToStudentEnrolments(enrolment)
Then now or later, a grade can be applied to the enrolled subject...
let grade: String = "A"
enrolment.grade = grade
Of course the average becomes a mathematical function based on the sum of all grades for each student, divided by the count. This is in my humble opinion, better constructed as it is needed, rather than saved as an attribute with each Student object.
Update
Im updating my answer to include a little database theory to explain my proposed object model.
According to Wikipedia, Database normalisation is...
the process of structuring a relational database
in accordance with a series of so-called normal forms in order to
reduce data redundancy and improve data integrity.
What does this practically mean to me? It means breaking my data down into its most discrete and unique parts, so that, in theory, I never need to enter any unique piece of data more than once.
Let me use a simple table example as a means of explaining this, as it might be set out in a spreadsheet (or your model concept):
Original Data
TABLE 1
A B C
1 STUDENT SUBJECT GRADE
2 Student1 Mathematics 8.8
3 Student1 Physics 7.0
4 Student1 Biology 6.0
5 Student2 Mathematics 5.0
6 Student2 Physics 9.0
7 Student2 Biology 7.0
Normalised Data
TABLE 1 TABLE 2 TABLE 3
A B C A B A B
1 STUDENT SUBJECT GRADE ID STUDENT ID SUBJECT
2 1 1 8.8 1 Student1 1 Mathematics
3 1 2 7.0 2 Student2 2 Physics
4 1 3 6.0 3 Biology
5 2 1 5.0
6 2 2 9.0
7 2 3 7.0
The normalised data uses relationships between the three tables. It stores the ID (as a primary key) of each STUDENT and each SUBJECT, instead of the actual words. This is obviously far more efficient in many different ways, including but not limited to: bytes of stored data, ability to index, speed of data retrieval.
When you set a relationship property in your Core Data object model graph, you are doing the same thing...
So for your example, the Core Data object model graph Entity replaces TABLE. The Core Data framework automagically inserts a primary key column into the SQLite database for us when it constructs the tables and later a primary key unique integer when we programmatically add rows (records, a.k.a instances of an entity). While we don't have direct access to that as a developer (using Core Data), the Core Data framework allows us to build one-to-one, one-to-many and many-to-many relationships between two entities, that achieves the same outcome.
Enrolment Student Subject
A B C A B A B
Rel. Rel. Att. Rel. Att. Rel. Att.
∞ ∞ 1 1
Z_PK Student Subject grade Z_PK name Z_PK name
1 1 1 8.8 1 Student1 1 Mathematics
2 1 2 7.0 2 Student2 2 Physics
3 1 3 6.0 3 Biology
4 2 1 5.0
5 2 2 9.0
6 2 3 7.0
Att. = Entity Attribute;
Rel. = Entity Relationship;
∞ = many side of one-to-many Relationship (<<-);
1 = one side of one-to-many Relationship (-->)
Any questions, let me know?

Two columns of a table referring to PK of another table in JPA

We have two tables
Table1(Name=Comment, PK is "ID"): (Lookup table)
ID Description Type
​1 Desc1 Type1
2 Desc2 Type2
Table2(Name=Steps, PK is "ID"):(Values has to be persisted and retrieved)
ID Value Comment1 Comment2
1 Value1 1 2
2 Value2 1 NULL
Comment1 and Comment2 are individually referring to ID of table1.
For each step in table 2 there might be different combinations of comment1 and comment2 referring to table1 (Comment1-has values w.r.t table1 with type as Type1, similarly Comment2).
What is the relationship that has to be defined in JPA for the entity comment in entity Step? We tried with ManyToOne on comment1 and comment2 of Step and had the following issues when retrieving the data(Although there was no issue when saving the data),
When retrieving data from Step, the description of comment is not being fetched on certain case where the data was retrieved immediately after persisting. But the value was fetched after the server was restarted. Do we have to explicitly join the tables when fetching(tried left join using criteria query). Should we have to edit the fetch type(tried both eager and lazy). Or does it have to do anything with cache refresh hints?
Annotation currently used,
#ManyToOne(fetch =FetchType.Eager, optional = false, cascade = CascadeType.REFRESH)
Your design is not normalized. You should add an intermediate table between comment and steps table.
comment table
ID Description Type
​1 Desc1 Type1
2 Desc2 Type2
step_comment table
step_id comment_id
1 1
1 2
2 1
step table
ID Value
1 Value1
2 Value2
You should consider using many to many association. Below is the example of many to many bidirectional association between 2 entity:
#Entity
public class Step {
#Id
#Column(name="ID")
private long id;
...
#ManyToMany
#JoinTable(
name="step_comment",
joinColumns=#JoinColumn(name="step_id", referencedColumnName="ID"),
inverseJoinColumns=#JoinColumn(name="comment_id", referencedColumnName="ID"))
private List<Comment> comments;
.....
}
#Entity
public class Comment {
#Id
#Column(name="ID")
private long id;
...
#ManyToMany(mappedBy="comments")
private List<Step> steps;
...
}
Please refer to the JPA Wiki for more information: https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

Querying Orient DB with 'joins'

I've recently started looking at OrientDB coming from a relational background (Oracle RDBMS) and I'm struggling to query some data I've loaded into OrientDB.
I have 2 classes:
CREATE CLASS prices
CREATE PROPERTY prices.price_created_datetime DATETIME
CREATE PROPERTY prices.price_value DOUBLE
CREATE CLASS stocks
CREATE PROPERTY stocks.stock_symbol STRING
CREATE PROPERTY stocks.stock_prices LINKLIST prices
I'm loading some data by first running an UPSERT on the 'stocks' class, and then several times over the day adding prices:
UPDATE stocks ADD stock_prices = {json string of class:prices}
What I'd like to do is get all stocks.stock_symbol values and aggregate (using average) the prices.price_value within the last 24 hours (therefore filtering on prices.price_created_datetime).
I'm using the web based studio and I've toyed with a few different methods but I'm struggling to get the concept when most of my queries return nothing. I have OrientDB 2.1.9 running embedded in a Java application.
Any help would be appreciated.
I tried your case with this structure (like yours):
Class: stocks
Property: stocks.stock_symbol STRING
Property: stocks.stock_prices LINKLIST prices
Class: prices
Property: prices.price_created_datetime DATETIME
Property: prices.price_value DOUBLE
And here's the data:
To find all of stock symbols with relative prices average of the last 24 hours, I used this query:
select stock_symbol, $prices.averageLast24 as averagePricesLast24 from stocks
let $prices = (select avg(price_value) as averageLast24 from (select price_value, price_created_datetime.asDatetime() as dataLast24 from prices) where eval('(sysdate() - dataLast24) / 3600000') < 24 and dataLast24 in $parent.current.stock_prices.price_created_datetime)
unwind averagePricesLast24
and this is the output:
----+------+------------+-------------------
# |#CLASS|stock_symbol|averagePricesLast24
----+------+------------+-------------------
0 |null |bbb |492345.5
1 |null |ccc |320167.0
----+------+------------+-------------------
Hope it helps

What column type is required when using ElementCollection with jpa?

I have a simple class with a column that is technically a List
#Entity
#Table(name='hat')
class Hat {
#Id
String id = UUID.randomUUID()
#ElementCollection
List<String> wat
}
Right now when I use either a varchar(500) or character varying(500) the PSQL code that pulls this entity out of the database explodes
org.postgresql.util.PSQLException: ERROR: relation
"hat_wat" does not exist
An element collection is not stored in a single column. It's stored in a separate table, just as if you have a OneToMany, except there is no identifier for the many side. Suppose the Hat foo has 2 wats "bar" and "baz":
hat table: hat_wat table:
id hat_id wat
----- ---------------
foo foo bar
foo baz
That allows querying the hats to find, for example, all the hats having "bar" in their list of wats. Trying to store multiple string in a single column is really not a good idea. It violates the first normal form rules.

Use view with Entity framework

I’m developing an application where I’m using Entity Framework. I have a table A and an autogen entity from this table class A
Public Class A
ID As Integer
Sum As Integer
TotalSum As Integer
LastPayment As Integer
NewPayment As Integer
.
.
.
End Class
In addition to my table I have a view that calculates and returns all the rows from table A where totalSum and LastPayment meets some conditions (table has 50 rows, view returns 35 rows).
Can I use this view together with my entity class A? When I use my entity class A I can say
unitOfWork.ARepository.Filter(Function(p) p.ID = Me._id, , )
but this will get the rows from the table without the calculations/filtering done by the view, let say it returns 50 row. I want to say
unitOfWork.ARepository.Filter(Function(p) p.ID = Me._id, , )
but I want to get the filtered rows from the view instead, this will return 35 rows instead of the 50. But I do not want the view to be an entity in my model, because I then will have two classes A (from table) and B (from view) that looks exactly the same. How can I solve this?
you can write a code corresponding view with entity framework in VB or C#. it's better than use 2 model that are equal.