Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kawodyaarachchige/hql-assignment-hibernate
Assignment: ORM Supplementary Assignment
https://github.com/kawodyaarachchige/hql-assignment-hibernate
Last synced: 8 days ago
JSON representation
Assignment: ORM Supplementary Assignment
- Host: GitHub
- URL: https://github.com/kawodyaarachchige/hql-assignment-hibernate
- Owner: kawodyaarachchige
- Created: 2024-02-13T20:51:42.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-02-13T20:52:38.000Z (11 months ago)
- Last Synced: 2024-11-09T06:06:42.844Z (2 months ago)
- Language: Java
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Assignment: ORM Supplementary Assignment
Q1
String hql = "from Books b where b.publicationYear > 2010";
Query query = session.createQuery(hql);
List list = query.list();for(Books b:list){
System.out.println(b.getId()+" "+b.getTitle()+" "+b.getPublicationYear()+" "+b.getPrice());
}Q2
String hql = " UPDATE Books b SET price = b.price /100 *110 WHERE b.author.id = :id";
Query query = session.createQuery(hql);
query.setParameter("id", 1);
int isUpdated = query.executeUpdate();
System.out.println(isUpdated> 0 ? "Updated" : "Not Updated");Q3
String hqlDeleteBooks = "delete from Books b where b.author.id = :authorId";
Query queryDeleteBooks = session.createQuery(hqlDeleteBooks);
queryDeleteBooks.setParameter("authorId", 1);
int deletedBooksCount = queryDeleteBooks.executeUpdate();
System.out.println("Deleted " + deletedBooksCount + " associated books");String hqlDeleteAuthor = "delete from Author a where a.id = :authorId";
Query queryDeleteAuthor = session.createQuery(hqlDeleteAuthor);
queryDeleteAuthor.setParameter("authorId", 1);
int deletedAuthorCount = queryDeleteAuthor.executeUpdate();
System.out.println(deletedAuthorCount > 0 ? "Author deleted" : "Author not found");Q4
String hql = " select avg(b.price) from Books b";
Query query = session.createQuery(hql);
double avg = (double) query.getSingleResult();
System.out.println(avg);Q5
String hql = "select a.name , count(b) from Author a left join a.books b group by a.name";
Query query = session.createQuery(hql);
List list = query.list();for(Object[] obj:list){
System.out.println(obj[0]+" "+obj[1]);Q6
String hql = "select a.name, b.title from Author a inner join a.books b where a.country =:country";
Query query = session.createQuery(hql);
query.setParameter("country", "Pakistan");
List list = query.list();
for (Object[] obj:list){
System.out.println(obj[0]+" "+obj[1]);Q7
@JoinColumn(name = "author_id") private Author author;Q10
Query query = session.createQuery("SELECT a.name FROM Author a WHERE ( SELECT COUNT(b.id) FROM Books b" +
" WHERE a.id = b.author.id ) > ( SELECT AVG(authorBookCount) FROM ( SELECT COUNT(b.id) AS authorBookCount " +
"FROM Books b GROUP BY b.author.id ))");
List resultList = query.getResultList();for (String s : resultList) {
System.out.println(s);
}