索引嵌入式ID Lucene

时间:2018-10-26 06:56:52

标签: java hibernate lucene

我是Lucene的新手。我有一个实体

@Indexed
@Table
@Entity
public class PerEntity {

    /** The id. */
    @EmbeddedId
    @IndexedEmbedded
    @SortableField
    @FieldBridge ( impl = KeyFieldBridge.class )
    private PrimaryKey composedId;

public PrimaryKey getComposedId() {
        return composedId;
    }

    public void setComposedId( PrimaryKey composedId ) {
        this.composedId = composedId;
    }
}

和PrimaryKey,不包括getter和setter。

@Embeddable
public class PrimaryKey implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The id of object .Database primary key */
    @Column ( name = "id" )
    @Type ( type = "uuid-char" )
    private UUID id;

    /** The version id of object.Database primary key */
    @GeneratedValue ( strategy = GenerationType.AUTO )
    @Column ( name = "version_id" )
    private int versionId;
}

主要方法和其他方法是

public static void main( String[] args ) {
        FullTextSession fullTextSession = Search.getFullTextSession( SuSHibernateUtilDB.buildSessionFactory().openSession() );
        try {
            fullTextSession.createIndexer().startAndWait();
        } catch ( Exception e ) {
            ExceptionLogger.logException( e, e.getClass() );
        }
        QueryParser luceneParser = new MultiFieldQueryParser( getEntityFields(), analyzer );
        org.apache.lucene.search.Query parse = luceneParser.parse( "*" );
        org.hibernate.search.FullTextQuery hibQuery = fullTextSession.createFullTextQuery( parse, clazz );
        List< ? > list = ( List< ? > ) hibQuery.getResultList();
    }

private static String[] getEntityFields() {
    List< String > allFieldNames = getLuceneIndexedFields( PerEntity.class );
    return allFieldNames.toArray( new String[ 0 ] );
}

也有一个场桥课程

public class KeyFieldBridge implements TwoWayFieldBridge {

    /**
     * {@inheritDoc}
     */
    @Override
    public void set( String name, Object value, Document document, LuceneOptions luceneOptions ) {
        if ( value != null ) {
            PrimaryKey primaryKey = ( PrimaryKey ) value;
            luceneOptions.addFieldToDocument( name, primaryKey.getId().toString(), document );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object get( String name, Document document ) {
        return UUID.fromString( document.get( name ) );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String objectToString( Object object ) {
        PrimaryKey primaryKey = ( PrimaryKey ) object;
        return primaryKey.getId().toString();
    }

我不知道该怎么办?哪些注释用于索引compositionId的ID?以及如何对ID进行排序? 当我运行main方法时,出现异常,即

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.util.UUID de.isko.software.simuspace.suscore.data.entity.PrimaryKey.id] by reflection for persistent property [de.isko.software.simuspace.suscore.data.entity.PrimaryKey#id] : 2c370ab9-3335-4e35-ab4a-7c6c3ffc024b
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.list(FullTextQueryImpl.java:234)
    at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.getResultList(FullTextQueryImpl.java:124)

请寻求任何帮助

0 个答案:

没有答案