I am unable to save child class data with the persistence of parent class using astyanax in cassandra.
I created the child object with all necessary data, but when I try to store that object, only values from the parent class is stored, not from child object.
Here is the sample Code not real:
#Entity
class Shape{
#Id
private String id;
#Column
private String name;
}
#Entity
class square extends Shape{
#column
private int width;
}
now to store I am using EntityManager of astyanax.
square s=new square();
s.setName("sqaure");
s.setWidth(100);
s.setId("1234");
EntityManager em= //initialization code
em.put(s);
after doing this only "name" and "id" is stored into database. not width.
The EntityManager requires the type of the entity via the withEntityType() method. This type is used to build an EntityMapper via reflection which then determines the fields to serialize. There is nothing in the Entity persistence documentation or examples that says Astyanax supports polymorphism. This is not a bug, just a feature that doesn't exist. You will need a type-specific EntityManager for each subtype of your base class.
Related
I have created a UDT named widgetData in cql for which i have a corresponding POJO class named widgetData. I want to use this in another domain POJO class as List. What kind of annotation should be used to do so?
#Table("dashboardManagement")
public class Dashboard implements Serializable {
#Column("dashboardState")
#CassandraType(type = DataType.Name.UDT, userTypeName = "widgetData")
private List<widgetData> dashboardState;
....
The above code does not work.
Do I have to write a seperate userTypeResolver for this?
I realize this question is a little old, but I have made this work.
Basically, I had a user profile with an address UDT, and that UDT had its own POJOs/entity classes. The UDT address entity class used the #UserDefinedType annotation:
#UserDefinedType("address")
public class AddressEntity implements Serializable {
private static final long serialVersionUID = 1817053316281666003L;
#Column("mailto_name")
private String mailtoName;
private String street;
private String street2;
private String city;
...
The user entity utilized the Address UDT entity:
#Table("user")
public class UserEntity implements Serializable {
private static final long serialVersionUID = 4067531918643498429L;
#PrimaryKey("user_id")
private UUID userId;
#Column("user_email")
private String userEmail;
#Column("first_name")
private String firstName;
#Column("last_name")
private String lastName;
#Column("addresses")
private List<AddressEntity> addresses;
...
Then, it was a simple matter to map a user's address data to a UserEntity object (userE below) and save it with standard repository methods.
// save to DB
userRepo.save(userE);
You can find everything built to support the User services here: https://github.com/datastaxdevs/workshop-ecommerce-app/blob/main/backend/src/main/java/com/datastax/tutorials/service/user/
So I would say to have a look at the class for the widgetData object, make sure it's using the #UserDefinedType annotation, and mark the column using the #Column annotation in the Dashboard class (basically, get rid of the #CassandraType):
#Column("dashboardState")
private List<WidgetData> dashboardState;
I am using spring data cassandra, and i have a #Table as defined below.
#Table(CassandraConstants.NotificationThread.NAME)
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
#PrimaryKey
private EventKey primaryKey;
#Column(value = CassandraConstants.Event.COL_COMPONENT_TYPE)
private ComponentType componentType;
...
}
In my dao code i am doing setting the enum value and doing a save. but i get an error.
event.setComponentType(ComponentType.CONNECTOR);
....
this.eventDao.save(event);
but i see this error reported while doing the save action
Invalid value CONNECTOR of type unknown to the query builder...
Does the spring Data not handle the conversion of enums to string data type for cassandra ?
Any pointers to what is failing here.
Are you using the official spring-data-cassandra project, (repo at https://github.com/spring-projects/spring-data-cassandra), that's under the Spring Data umbrella of projects, or a different project?
I've seen a lot of Kundera examples where the object being store is fairly simple. You have something like a Car.class and it contains a couple String variables maybe an int mapped using the #Column annotation. I've even seen some List, Set and Map variables as well as the cqlsh to create a column of those types.
What I haven't seen is a custom object I created within an object and how that would be represented in a Cassandra DB.
For example:
public Class ContainerShip {
#Column(name="container")
Container myContainer;
}
public Class Container {
#Column(name="containerName)
String containerName;
}
Could I store ContainerShip into Cassandra, using Kundera with em.persist(myShip)?
If I can what would the cqlsh for creating the "container" column look like?
You may embed a container object as an embeddable entity.
#Entity
public Class ContainerShip {
#Column(name="container")
#Embedded
Container myContainer;
}
#Embeddable
public Class Container {
#Column(name="containerName)
String containerName;
}
Consider the following class:
class MyContext : DbContext
{
public DbSet<Order> Orders { get; set; }
}
and instantiating a new object:
var mycontext = new MyContext();
Why mycontext.Orders is not null? When it was initialized? Who has initialized it? I'm really confused because the base class (DbConetxt) cannot access the derived class properties so it is not possible that the automatic property was initialized in the base object.
From looking at the reflected code, when the DbContext (the base class) is constructed it makes a call to the DbSetDiscoveryService (an internal clasS) - which essentially uses reflection to find all properties on the DbContext, and then initializes those that need initializing.
So in short - using reflection in the constructor.
I need to display a large table with about 1300 roles at one time. (I know I should use a data scroll but my users want to see the whole table at one time.) The table displays 4 columns. Two of those columns are from the object but the other two are from referenced objects in the original object. I need to find the best/efficient way to do this. I currently have this working but when I reload the table it give an out of memory error. I think that it's caused by the large amount of redundant data in memory.
Create a view object that the repository will fill in only the needed fields.
Any other suggestions.
Here are the objects:
public class Database extends EntityObject {
private Long id;
private String name;
private String connectionString;
private String username;
private String password;
private String description;
// getter and setters omitted
}
public class Application extends EntityObject {
private Long id;
private String name;
private String fullName = "";
private String description;
private Database database;
private List<Role> roles = new ArrayList<Role>(0);
// getter and setters omitted
}
public class Role extends EntityObject {
private Long id;
private String name;
private String nameOnDatabase;
private Application application;
// getter and setters omitted
}
What I need displayed from the list of Roles is:
role.id, role.name, role.application.name, role.application.database.name
To optimize wisely, define what are you going to do with data, view or/and edit. Here are some common scenarios:
Retrieval using lazy fetch type.
Mark your roles in application with FetchType.LAZY annotation.
Retrieval using multiselect query. Create your custom class (like DTO) and populate it with data from the database using multiselect query. (Similar to VIEW mapped as Entity)
There are also other possibilities, such as Shared (L2) Entity Cache or Retrieval by Refresh.
See if you are using EntityManager correctly reading Am I supposed to call EntityManager.clear() often to avoid memory leaks?.