I am using Cassandra 3.6 with Achilles. I have below designed table in Cassandra.
CREATE TABLE userVehicles (
userid text,
name text static,
surname text static,
vehicleNum text,
vehicleMake text,
vehicleModel text,
vehicleYear text,
PRIMARY KEY (userid)
);
I will be having many vehicles attached to single User and when I will select with UserId, I should get User details and all vehicle belongs to him.
How to map this in Achilles??
Updating table
CREATE TYPE Vehicle(
---------
)
And then in userVehicles table
vehicleDetails LIST<FROZEN<Vehicle>>
Tried Achillies mapping
#Table(keyspace="keyspace", table="userVehicles")
public class UserDetails{
#PartitionKey(value = 1)
private String userid ;
#Column
private String name ;
#Column
private String surname;
#Column
private List<#Frozen Vehicle> vehicle;
}
#UDT(keyspace="keyspace", name="vehicle")
public class Vehicle {
#Column
private String vehicleNum ;
#NotNull
#Column
private String vehicleMake ;
#Column
private String vehicleModel ;
#NotNull
#Column
private String vehicleYear ;
}
#Configuration
#CompileTimeConfig(cassandraVersion = CassandraVersion.CASSANDRA_3_7)
public class AchillesConfiguration {
#Autowired
ManagerFactory managerFactory;
#Bean
public User_Manager userEntityManager(){
return managerFactory.forUser();
}
}
Answer is updated in mapping above. For nested tables, this is the way you should go in Cassandra with Achilles framework.
Related
Creating JPA model class for a table which does not have primary key defined but has unique index created .
For example there is a table popular_item table which has columns like itemEntityId, itemType, itCode,quantity, status,createdDate.
Among these if the columns itemEntityId, itemType, itCode,quantity are part of the unique index . Then how to create the Model class ?
The JPA model class can be created as below with an ID as PopularItemId which is combination of the columns which are part of the unique index
#Table(name="popular_item")
#Entity
#NoArgConstructor
#AllArgCOnstructor
PopularItem{
#EmbeddedId
#AtrributeOverride(name="itemEntityId",column=#Column(name=""))
#AtrributeOverride(name="itemType",column=#Column(name="itemType"))
#AtrributeOverride(name="itCode",column=#Column(name="itCode"))
#AtrributeOverride(name="quantity",column=#Column(name="quantity"))
private PopularItemId popularItemId;
#Column(name="status")
private boolean status;
#Column(name="created_date")
private LocalDate createdDate;
}
#AllArgsConstructor
#NoArgConstrucotor
#Embeddable
public class PopularItemId {
private String itemEntityId;
private String itemType;
private String itCode;
private Double quantity;
}
I want to insert doctor object to database, how should I put annotations for properties?
I tried to do it with te code shown below.
But i don't know how to do it on list properties specializations and phoneNumbers.
#Table(databaseName = WMDatabase.NAME)
public class Doctor extends BaseModel{
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Column
private FullName fullName;
#Column
private String organizationId;
#Column What shuld i put here?????
private List<Specialization> specializations;
#Column What shuld i put here?????
private Contacts contacts;
}
Below are the classes I use for doctor attributes:
public class Contacts extends BaseModel {
private List<PhoneNumber> phoneNumbers;
private String email;
private String fax;
}
public class Specialization extends BaseModel {
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Unique(unique = true)
private String specializationName;
public String getSpecializationName() {
return specializationName;
}
public void setSpecializationName(String specializationName) {
this.specializationName = specializationName;
}
DBFlow is a relational database system (not a mongo-type key/value store) and doesn't support lists as columns, according to the doc here.
List : List columns are not supported and not generally proper for a relational database. However, you can get away with a non-generic List column via a TypeConverter. But again, avoid this if you can.
The documentation on relationships may help you refine the model to suit your needs.
I have three classes corresponding to three tables in mysql database. My classes are as follows.
#Entity
#Table(name="location")
public class Location {
private Integer locationId;
private Integer hospitalId;
private Integer regionId;
private String locationCode;
private String locationName;
private String locationType;
#Entity
#Table(name="hospital_region")
public class HospitalRegion {
private Integer regionId;
private Integer hospitalId;
private String regionCode;
private String regionName;
public enum Status{Active,Inactive}
private Status status;
#Entity
#JsonAutoDetect
#Table(name="hospital_information")
public class HospitalInformation{
private Integer hospitalId;
private String shortName;
private String name;
private Integer packageId;
private Date implementationDate;
private Date validFrom;
private Date validUpTo;
private Date lastUpload;
public enum SubscriptionType{Free,Complimentary,Paid}
private Integer totalUsers;
I am making a Web Services for a Hospital Application where one region could have multiple locations(one-to-many) and one hospital could be in multiple regions(one-to-many).
So what I want to do is make a web service that would insert the data into location table.The ideal workflow should be that I shall pass every field in Location class as a json object to insert a record into the Location table.
My Business Logic should first check for my regionId and hospitalId value passed in the json object . If the hospitalId which is passed corresponds to the value of regionId in region table, if both correspond, only then data should be saved.
So I need help about how to implement it as a Business Logic.Thanks in advance
You miss the JPA relationships concept.
Your attributes are not annotated in the 3 classes.
You need to read about:
#ManyToOne Relation
#OneToMany Relation
#OneToOne Relation
#ManyToMany Relation
See more:
JPA Foreign Key Annotation
JPA Relationships 1
JPA Relationships 2
I'm brand new to Hibernate, and I'm trying to get a fairly simple Hibernate code snippet to work. After reading the tutorials, I'm totally choking on the full implementation.
For one, when it comes to the hbm2ddl.auto property, I'm setting it to validate because I just don't like the idea of Hibernate creating my table structure (I'm old fashioned; perhaps that will change as I become more comfortable with Hibernate though). In any event, here's the table I just created on a MySQL server:
CREATE TABLE users (
id INT NOT NULL AUTO INCREMENT,
email VARCHAR(200) NOT NULL,
title VARCHAR(25),
first_name VARCHAR(100),
middle_name VARCHAR(100),
last_name VARCHAR(100),
suffix VARCHAR(100),
PRIMARY KEY (id)
);
Thise corresponds to the following POJOs/entities in my app's code:
#Entity
#Table(schema="my_db", name="users")
public class User {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="id")
private Integer id;
#Column(name="email")
private String email;
// ???
private PersonName personName;
public User(final String email, final PersonName personName) {
super();
setEmail(email);
setPersonName(personName);
}
// Getters and setters omitted for brevity...
}
public abstract class BaseName {
public abstract String toName();
#Override
public String toString() {
return toName();
}
}
public class PersonName extends BaseName {
private String title;
private String firstName;
private String middleName;
private String lastName;
private String suffix;
public PersonName(final String title, final String firstName, final String middleName, final String lastName, final String suffix) {
super();
setTitle(title);
setFirstName(firstName);
setMiddleName(middleName);
setLastName(lastName);
setSuffix(suffix);
}
// Getters and setters omitted for brevity...
}
What annotations/config do I need to add so that User#personName gets persisted as an embedded PersonName object inside the users table? In other words, User is an entity and contains a PersonName as an embedded objects (non-entity).
Also, any other obvious annotations I'm missing? Thanks in advance!
As suggested by user #Snow Blind, Embedded objects are what you want.
Let's start with class PersonName:
#Embeddable
public class PersonName extends BaseName {
#Column(name = "title")
private String title;
#Column(name = "first_name")
private String firstName;
#Column(name = "middle_name")
private String middleName;
#Column(name = "last_name")
private String lastName;
#Column(name = "suffix")
private String suffix;
// ...
}
Now add the #Embedded tag here:
// ...
#Embedded
private PersonName personName;
// ...
Hope this helps!
You can check the title 2.2.2.4. Embedded objects (aka components) in docs:
http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-property
The following query throws the exception:
Query query = session.createQuery("from Associate as a order by a.username asc");
associates = query.list();
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [ca.mypkg.model.Associate#0]
If I create an entry in the database with id of 0 it works just fine. I don't really get it because I'm just trying to load all the entries in the db not just a specific one.
Similar questions I've found have been concerned with trying to load an object with a given ID I'm doing no such thing.
Associate class:
#Table(name = "user")
#XmlRootElement(name = "associate")
public class Associate implements Serializable {
private String username;
private String password;
private String firstName;
private String lastName;
private String userType;
private int id;
private String email;
private String isActive;
private Department dept;
private String lastUpdated;
private String associate_type;
// ...
#Id
#GeneratedValue
public int getId() {
return id;
}
#OneToOne
#JoinColumn(name = "dept")
public Department getDept() {
return dept;
}
From my experience this type of error message usually means it does not find joined entity by mentioned id, and not the entity requested in the query (Associate, in your case).
My guess is that Associate class contains a join entity which has primitive type primary key.