-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Hibernate Search Example
PostPosted: Wed Nov 28, 2007 11:26 pm 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
I just start learn about Hibernate and Spring. Now i am studying about hibernate search and i find an example about hibernate search.
Anyone who have an example about hibernate search can give me???

thanks a lot
sorry about my English


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 2:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The reference documentation is a good start
http://www.hibernate.org/hib_docs/search/reference/en/html_single/

There are also 2 examples in the JBoss Seam 2.0.x distribution (examples/dvdstore and examples/blog)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 10:38 pm 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
I use hibernate in Quickstart Example and i code Person class as:

package quickstart.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.search.annotations.*;

@Entity
@Indexed(index="indexes/Person")
public class Person {
@Id
@GeneratedValue
@DocumentId
private Integer id;

@Field(index=Index.TOKENIZED, store=Store.NO)
private String firstName;

@Field(index=Index.TOKENIZED, store=Store.NO)
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}

and i implements PersonService as below:

package quickstart.service;

import java.awt.print.Book;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.springframework.transaction.annotation.Transactional;

import quickstart.model.Person;

@Transactional
public class PersonServiceImpl implements PersonService {
private EntityManager em;

@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}

@SuppressWarnings("unchecked")
public List<Person> findAll() {
Query query = getEntityManager().createQuery("select p FROM Person p");
return query.getResultList();
}

public void save(Person person) {
if (person.getId() == null) {
// new
em.persist(person);
} else {
// update
em.merge(person);
}
createIndex();
}

public void remove(int id) {
Person person = find(id);
if (person != null) {
em.remove(person);
}
createIndex();
}

private EntityManager getEntityManager() {
return em;
}

public Person find(int id) {
return em.find(Person.class, id);
}

public void createIndex(){
FullTextEntityManager fullTextEntityManager = Search.createFullTextEntityManager(em);
List<Person> persons = em.createQuery("select person from person as person").getResultList();
for (Person person : persons) {
fullTextEntityManager.index(person);
}
}

public List<Person> search(String searchString){

FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.createFullTextEntityManager(em);
MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{"firstname", "lastname"},
new StandardAnalyzer());
Query query = parser.parse(searchString);
org.hibernate.Query hibQuery = fullTextEntityManager.createFullTextQuery( query, Person.class );
List result = hibQuery.list();
}
}

and MyEclipse throw error as below :

-The method createFullTextQuery(Query, Class...) in the type FullTextEntityManager is not applicable for the arguments (Query, Class<Person>)
-Severity and Description Path Resource Location Creation Time Id
Type mismatch: cannot convert from Query to Query

What is thas error?? Can you show me ???
thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 10:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Be sure to not have any old jar including hibernate annotations or hibernate search in your classpath

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 11:00 pm 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
I ran the project quickstart. I can create index vĂ  search on index. But when i insert a row into table then it throw an error :
java.lang.LinkageError: loader constraint violation: when resolving interface method "org.hibernate.Transaction.registerSynchronization(Ljavax/transaction/Synchronization;)V" the class loader (instance of org/jboss/web/tomcat/service/WebAppClassLoader) of the current class, org/hibernate/search/backend/impl/TransactionalWorker, and the class loader (instance of org/jboss/mx/loading/UnifiedClassLoader3) for resolved class, org/hibernate/Transaction, have different Class objects for the type javax/transaction/Synchronization used in the signature


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 11:01 pm 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
can you help me ? thank a lot !!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 11:38 pm 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
This is my source code :
http://www.uploadfree.org/931457
I use JBoss 4.2.1.GA and
Hibernate Annotations 3.3.0.GA
Hibernate Commons Annotations 3.0.0.GA
Hibernate EntityManager 3.3.1.GA
Hibernate Search 3.0.0.GA

My classpath as below
CLASSPATH = ";C:\Program Files\Java\jdk1.6.0_02\bin;C:\Library\hibernate-search.jar;C:\Library\lucene-core-2.2.0.jar"

But when i run project then it threw an exception such as:

10:14:29,398 ERROR [[default]] Servlet.service() for servlet default threw exception
java.lang.LinkageError: loader constraint violation: when resolving interface method "org.hibernate.Transaction.registerSynchronization(Ljavax/transaction/Synchronization;)V" the class loader (instance of org/jboss/web/tomcat/service/WebAppClassLoader) of the current class, org/hibernate/search/backend/impl/TransactionalWorker, and the class loader (instance of org/jboss/mx/loading/UnifiedClassLoader3) for resolved class, org/hibernate/Transaction, have different Class objects for the type javax/transaction/Synchronization used in the signature.

can you show me the problem??????
thank a lot!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 8:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
try to update hibernate-search.jar, hibernate annoationds hibernate entitymanager and hibernate commons annotations jars from JBoss AS
in default/server sor something like that.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 10:25 pm 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
i ran the project normally.
Thank you so much.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 5:36 am 
Newbie

Joined: Wed Nov 28, 2007 10:04 pm
Posts: 10
i upgrade my project and i want to use Embedded and associated.
I have 2 class are Person and Book. a book will have an author. Book will have relationship one - one with Person and Person will have relationship one - many with Book. 2 class as below :

Person.java
Code:
package quickstart.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.mapping.Set;
import org.hibernate.search.annotations.*;
import quickstart.model.Person;

@Entity
@Indexed(index="indexes/Person")
public class Person {
   
    @Id
    @GeneratedValue
    @DocumentId
    private Integer id;
   
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String firstName;
   
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String lastName;
   
    @ContainedIn
    @OneToMany(mappedBy = "Person")
    private Set<Book> books;


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}


and Book.java
Code:
package quickstart.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.hibernate.annotations.CascadeType;
import org.hibernate.search.annotations.*;

@Entity
@Indexed(index="indexes/Book")
public class Book {
   
    @Id
    @GeneratedValue
    @DocumentId
    private Integer id;
   
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String name;
   
    @OneToOne( cascade = { CascadeType.PERSIST, CascadeType.REMOVE } )
    @IndexedEmbedded
    private int author;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAuthor() {
        return author;
    }

    public void setAuthor(int author) {
        this.author = author;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}


but it throw 5 errors as below:
Code:
-Type mismatch: cannot convert from CascadeType to CascadeType
-The type Set is not generic; it cannot be parameterized with arguments <Book>
-Target null.class is not an entity
-Target null.class is not an entity
-MappedBy specified for books does not exist on the target entity class


can you show me the problem????
thx[/code][/quote]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.