Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java hibernate questions: book.java import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Embeddable; import javax.persistence.Id; import

Java hibernate questions:

book.java

import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Embeddable; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.persistence.Entity; import javax.persistence.FetchType; @Entity @Table(name = "book") // POJO class public class book { @Id @Column(name = "bookId") public int id; @Column(name = "bookName") public String bookName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "book_tag", joinColumns = @JoinColumn(name = "b__id",referencedColumnName="bookId")) @Column(name = "b_tag") private Set tags = new HashSet(); public Set getTags() { return tags; } public void setTags(Set tags) { this.tags = tags; } public List getResultList() { // TODO Auto-generated method stub return null; } }

App.java

import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; // Main class public class App { Configuration c = new Configuration(); SessionFactory sessionFactory1 = c.buildSessionFactory(); public static void mainTemp(String[] args) { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); configuration.addAnnotatedClass(book.class); configuration.addAnnotatedClass(Tag.class); configuration.addAnnotatedClass(LitItem.class); // Create Session Factory SessionFactory sessionFactory = configuration.buildSessionFactory(); // Initialize Session Object } // Main driver method public static void main(String[] args) { // Create Configuration Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); configuration.addAnnotatedClass(book.class); // Create Session Factory SessionFactory sessionFactory = configuration.buildSessionFactory(); // Initialize Session Object Session session = sessionFactory.openSession(); session.beginTransaction(); book maths = new book(); maths.setId(3); maths.setBookName("CSC"); Set tags = new HashSet(); tags.add("cs1"); tags.add("cs2"); tags.add("cs3"); maths.setTags(tags); session.save(maths); book science = new book(); science.setId(2); science.setBookName("science"); tags = new HashSet(); tags.add("bio"); tags.add("che"); tags.add("phy"); science.setTags(tags); session.save(science); session.getTransaction().commit(); session.beginTransaction(); Query query = session.createQuery("SELECT b FROM book b JOIN b.tags t ON b.id = t.b__id where t.b_tag=:b_tag ", book.class);

query.setParameter(" cs1", tags) ; List bList = query.getResultList(); for(book b : bList){ System.out.println(b.getBookName()); } session.getTransaction().commit();

}}

In the program there is an exception that there is problem of dereferencing data, , how to remove the error and execute the select query to obtain distinct output for a bookName with multiple tags. without using the keyword distinct? give output too

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

3031143426, 978-3031143427

More Books

Students also viewed these Databases questions