Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!