Hello!

There's an answer on stack overflow:
https://stackoverflow.com/questions/66716680/how-to-define-ignite-caches-for-objects-joined-from-multiple-tables/66764133#66764133

If you have further questions, you can ask here as well.

Regards,
-- 
Ilya Kasnacheev


сб, 20 мар. 2021 г. в 02:05, PunxsutawneyPhil3 <rwoodr...@niksun.com>:

> I am working with Postgres as an external persistent store Ignite and want
> to
> know the best way to define caches for objects who’s data is spread over
> multiple tables.
>
> E.G. for to work with this Person and Car class and their tables below, I
> have provided my own Implementation of the CacheStore.  This approach seems
> to be very verbose however as I need to manually assign the field values
> myself.  Are there any other methods I should be using to do this?
>
> public class PersonMO{
>         private int id;
>         private String name;
>         private String address;
>         private Set<Car> cars
>
>         public PersonMO() {};
>         public PersonMO(int id, String name, String address) {
>                 this.id = id;
>                 this.name = name;
>                 this.address = address;
>         }
>
>         public String getName() {
>                 return name;
>         }
>
>         public void setName(String name) {
>                 this.name = name;
>         }
>
>         public int getId() {
>                 return id;
>         }
>
>         public void setId(int id) {
>                 this.id = id;
>         }
>
>         public String getAddress() {
>                 return address;
>         }
>
>         public void setAddress(String address) {
>                 this.address = address;
>         }
>
>         public String toString() {
>                 return "ID: "+id +", Name: "+name+" AD: " +address;
>         }
>
>         public void setCars(Set<Car> cars) {
>                 this.cars = cars;
>         }
>
>         public Set<Car> getCars() {
>                 return cars;
>         }
> }
>
> public class Car {
>         int id;
>         private String name;
>
>         public Car(int id, String name) {
>                 this.id = id;
>                 this.name = name;
>         }
>
>         public int getId() {
>                 return id;
>         }
>
>         public void setId(int id) {
>                 this.id = id;
>         }
>
>         public String getName() {
>                 return name;
>         }
>
>         public void setName(String name) {
>                 this.name = name;
>         }
>
> }
>
>
> public class PersonMOCacheStore implements CacheStore<Integer, PersonMO>{
>
>         @SpringResource(resourceName = "pgDataSource")
>         private DriverManagerDataSource pgDataSource;
>
>         @LoggerResource
>     private IgniteLogger log;
>
>         //public void loadCache(IgniteBiInClosure<Integer, PersonMO> clo,
> @Nullable
> Object... args)
>         @Override
>         public void loadCache(IgniteBiInClosure<Integer, PersonMO> clo,
> Object...
> args)
>                         throws CacheLoaderException {
>                 log.info(">> Loading cache from store...");
>
>                 try(Connection conn = pgDataSource.getConnection()){
>                         try(PreparedStatement st =
> conn.prepareStatement("select * from
> PERSON")){
>                                 try(ResultSet rs = st.executeQuery()){
>                                         while(rs.next()) {
>                                                 PersonMO person = new
> PersonMO(rs.getInt(1),rs.getString(2),
> rs.getString(3));
>
> person.setCars(getCarSet(conn, person.getId() ) );
>                                                 clo.apply(person.getId(),
> person);
>                                         }
>                                         log.info(">> Finished Loading
> cache from store...");
>                                 }
>                         }
>                 }catch(SQLException e) {
>                          throw new CacheLoaderException("Failed to load
> values from cache
> store.",e);
>                 }
>
>         }
>
>         //implementation for IgniteCache.get
>         @Override
>         public PersonMO load(Integer key) throws CacheLoaderException {
>                 log.info(">> Loading person from store...");
>
>                 try (Connection conn = pgDataSource.getConnection()) {
>                         try(PreparedStatement st =
> conn.prepareStatement("select * from PERSON
> where id = ?")){
>                                 st.setString(1, key.toString());
>                                 ResultSet rs = st.executeQuery();
>                                 if(rs.next()) {
>                                         PersonMO p= new
> PersonMO(rs.getInt(1),rs.getString(2),
> rs.getString(3));
>                                         //p.setCars( getCarSet(conn,
> p.getId() ) );
>                                         return p;
>                                 }else {
>                                         return null;
>                                 }
>
>
>                         }
>                 }catch(SQLException e) {
>                          throw new CacheLoaderException("Failed to load
> values from cache
> store.",e);
>                 }
>         }
>
>
>         private Set<Car> getCarSet(Connection conn, int personId) throws
> SQLException{
>                 Set<Car> carSet = new HashSet<Car>();
>                 PreparedStatement st = conn.prepareStatement("select *
> from CAR where id =
> "+ personId);
>                 ResultSet rs = st.executeQuery();
>
>                 while(rs.next()) {
>                         carSet.add(new Car(rs.getInt(1),rs.getString(2) ));
>                 }
>                 return carSet;
>         }
>
> }
>
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
>

Reply via email to