Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

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 book_tag t ON b.id = t.b__id where b.id=:id ", book.class); List bList = query.getResultList(); for(book b : bList){ System.out.println(b.getBookName()); } session.getTransaction().commit();

}}

In the program there is an exception that Could not resolve entity reference: book_tag , how to remove the error and execute the select query to obtain distinct output 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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

RP-1 What is the value of finding flow in our work?

Answered: 1 week ago

Question

The nature and importance of the global marketplace.

Answered: 1 week ago