cassandra - accessing child constant in parent class in java -


ok, have interesting problem. using java/maven/spring-boot/cassandra... , trying create dynamic instantiation of mapper setup use. i.e.

//users.java import com.datastax.driver.mapping.annotations.table;  @table(keyspace="mykeyspace", name="users") public class users {     @partitionkey     public uuid id;     //... } 

now, in order use have explicitly ...

users user = (db).mapper(users.class); 

obviously replacing (db) db class. great model, running problem of code repetition. cassandra database has 2 keyspaces, both keyspaces have exact same tables exact same columns in tables, (this not choice, absolute must have according company). when need access 1 or other based on form submission becomes mess of duplicated code, example:

//mywebcontroller.java import ...;  @restcontroller public class myrestcontroller {  @requestmapping(value="/orders", method=requestmethod.post) public string getorders(...) {     if(objects.equals(client, "first_client_name") {         //do things first keyspace objects like....         firstclientusers users = (db).mapper(firstclientusers.class);         //...     } else if(objects.equals(client, "second_client_name") {         secondclientusers users = (db).mapper(secondclientusers.class);         //....     }     return ""; } 

i have been trying use methods like...

class cls = class.forname(string_input_variable_here); 

and works ok base classes when trying use accessor stuff no longer works because accessors have interfaces, when class cls, no longer interface.

i trying find other solution on how dynamically have work , not have have duplicate code every possible client. each client have it's own namespace in cassandra, exact same tables other ones.

i cannot change database model, must according company. php extremely simple since doesn't care typecasting much, can do...

function getdata($name) {     $classname = $name . 'accessor';     $class = new $classname(); } 

and poof have dynamic class, problem running type specification have explicitly say...

firstclientusers users = new firstclientusers(); //or firstclientusers users = class.forname("firstclientusers"); 

i hope making sense, can't imagine first person have problem, can't find solutions online. hoping knows how can accomplished without duplicating exact same logic every single keyspace have. makes code not maintainable , unnecessarily long.

thank in advance can offer.

do not specify keyspace in model classes, , instead, use so-called "session per keyspace" pattern.

your model class (note keyspace left undefined):

@table(name = "users") public class users {     @partitionkey     public uuid id;     //... } 

your initialization code have this:

map<string, mapper<users>> mappers = new concurrenthashmap<string, mapper<users>>();  cluster cluster = ...;  session firstclientsession = cluster.connect("keyspace_first_client"); session secondclientsession = cluster.connect("keyspace_second_client");  mappingmanager firstclientmanager = new mappingmanager(firstclientsession); mappingmanager secondclientmanager = new mappingmanager(secondclientsession);  mappers.put("first_client", firstclientmanager.mapper(users.class)); mappers.put("second_client", secondclientmanager.mapper(users.class));  // etc. clients 

you store mappers object , make available through dependency injection other components in application.

finally, rest service this:

import ...  @restcontroller public class myrestcontroller {      @javax.inject.inject     private map<string, mapper<users>> mappers;      @requestmapping(value = "/orders", method = requestmethod.post)     public string getorders(...) {         mapper<users> usersmapper = getusersmapperforclient(client);         // process request right client's mapper     }      private mapper<users> getusersmapperforclient(string client) {         if (mappers.containskey(client))             return mappers.get(client);         throw new runtimeexception("unknown client: " + client);     } } 

note how mappers object injected.

small nit: name class user in singular instead of users (in plural).


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -