@IndexedEmbedded and @ContainedIn relationship not working

元气小坏坏 提交于 2019-12-11 15:16:44

问题


Data from one index "A" should be updated to another index "B" while using the @IndexedEmbedded and @ContainedIn relationship. But still the entity containing the @IndexedEmbedded annotation is not updated with data from entity containing the @ContainedIn annotation.

Not sure, what I'm doing wrong. But as per documentation, this should work. Once I update A the same should reflect on B.

The Elastic DB structure is, I believe fine. But the index is empty.

But the HibernateMasterSearchIndex is updated when I manually save the object to Elastic DB like

fullTextSession.save(localBasclt0900TO.getHibernateMasterSearch());

will I've to always persist/delete/update the data like above. If yes, then why the documentation of hibernate search says, it will work automatically.

I believe this is not the correct way of doing this.

Any ways, below is the code

    {
  "hibernatemastersearchindex" : {
    "aliases" : { },
    "mappings" : {
      "com.csc.pt.svc.data.to.HibernateMasterSearch" : {
        "dynamic" : "strict",
        "properties" : {
          "basclt0100TO" : {
            "properties" : {
              "clientname" : {
                "type" : "text"
              },
              "cltseqnum" : {
                "type" : "long",
                "store" : true
              },
              "firstname" : {
                "type" : "text"
              },
              "longname" : {
                "type" : "text"
              },
              "midname" : {
                "type" : "text"
              }
            }
          },
          "basclt0900to" : {
            "properties" : {
              "cltseqnum" : {
                "type" : "long",
                "store" : true
              },
              "email1" : {
                "type" : "text",
                "store" : true,
                "analyzer" : "standardAnalyzer"
              }
            }
          },
          "id" : {
            "type" : "keyword",
            "store" : true
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "number_of_shards" : "5",
        "provided_name" : "hibernatemastersearchindex",
        "creation_date" : "1534838340085",
        "analysis" : {
          "filter" : {
            "standardAnalyzer_PatternReplaceFilterFactory" : {
              "all" : "true",
              "pattern" : "([^a-zA-Z0-9\\.])",
              "type" : "pattern_replace",
              "replacement" : " "
            }
          },
          "analyzer" : {
            "standardAnalyzer" : {
              "filter" : [
                "word_delimiter",
                "lowercase",
                "standardAnalyzer_PatternReplaceFilterFactory"
              ],
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "Douqy81VQ7mLX-VWiFYAdQ",
        "version" : {
          "created" : "6030299"
        }
      }
    }
  }
}

Two entities are :

HibernateMasterSearch (which is to be updated)

@Entity
@Indexed(index = "HibernateMasterSearchIndex")
public class HibernateMasterSearch {

    @Id
    @GeneratedValue
    private Long id;


    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @IndexedEmbedded(prefix = "clt0100_")
    private Basclt0100TO basclt0100TO;

    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @IndexedEmbedded(prefix = "clt0900_")
    private Basclt0900TO basclt0900to ;

Basclt0900TO (which will provide the data)

 @OneToOne(mappedBy = "basclt0900TO")
    @ContainedIn
    private HibernateMasterSearch hibernateMasterSearch;    


    public HibernateMasterSearch getHibernateMasterSearch() {
        return hibernateMasterSearch;
    }
    public void setHibernateMasterSearch(HibernateMasterSearch hibernateMasterSearch) {
     this.hibernateMasterSearch = hibernateMasterSearch;
    }

Basclt0100TO (which will provide the data)

@OneToOne
@ContainedIn
private HibernateMasterSearch hibernateMasterSearch;    


public HibernateMasterSearch getHibernateMasterSearch() {
    return hibernateMasterSearch;
}
public void setHibernateMasterSearch(HibernateMasterSearch hibernateMasterSearch) {
    if(hibernateMasterSearch != null) {
        hibernateMasterSearch.setBasclt0100TO(this);
    }
    this.hibernateMasterSearch = hibernateMasterSearch;
}

Hibernate HBM for HibernateMasterSearch

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.csc.pt.svc.data.to.HibernateMasterSearch" table="HibernateMasterSearch">
         <id name="id" type="bphx.c2ab.hibernate.NullSafeLong" column="ID">
            <generator class="identity" />
         </id>
        </class>

  </hibernate-mapping>

Creating Initial Indexes

private void createInitialIndex() {
        Session session = HibernateSessionFactory.current().getSession("");
        FullTextSession fullTextSession = Search.getFullTextSession(session.getSession());
        try {

            fullTextSession.createIndexer().startAndWait();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Other than the above code, I'm not using the fulltextsearch session anywhere in my code.

hibernate.cfg.xml

<property name="hibernate.search.default.indexmanager">elasticsearch</property>
        <property name="hibernate.search.default.elasticsearch.host">http://127.0.0.1:9200</property>
        <property name="hibernate.search.default.elasticsearch.index_schema_management_strategy">drop-and-create</property>
        <property name="hibernate.search.default.elasticsearch.required_index_status">yellow</property>

Code to save A/Basclt0100

public static DBAccessStatus insert(Basclt0100TO basclt0100TO) {
    DBAccessStatus status = new DBAccessStatus();
    try {
        Session session = HibernateSessionFactory.current().getSession("BASCLT0100");
        if (Util.get(session, basclt0100TO, basclt0100TO.getId()) != null) {
            status.setSqlCode(DBAccessStatus.DB_DUPLICATE);
        } else {
            if (session.contains(basclt0100TO)) {
                session.evict(basclt0100TO);
            }
            LocalDateTime currentDate = LocalDateTime.now();
            Soundex soundex = new Soundex();    
            basclt0100TO.setPhnclient(soundex.encode(basclt0100TO.getClientname()));
            basclt0100TO.setPhnfirst(soundex.encode(basclt0100TO.getFirstname()));

            session.save(basclt0100TO);
            session.flush();
        }
    } catch (HibernateException ex) {
        status.setException(ex);
    }
    return status;
}

The same way B/Basclt0900 and C/Basclt0300 is persisted to database.

Should I persist these to ES DB at the same time. Not sure of how to handle/persist data in such a way that the composite index should get updated with the changes done on DB tables/indexes involved in composite index.

来源:https://stackoverflow.com/questions/51943651/indexedembedded-and-containedin-relationship-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!