In my application i have entities such as Organisations and Users .Every user belongs to an organisation. I want to implement two factor authentication for users based on Organisation settings Ip (i will save ip range in it) and Geolocation (I will save ,cityName, latitude ,longitude ,and radius).What is the best way to save these settings for an organisation.
My idea is to create one entity TwoFactorSettings and to store in it IpConfigs and Geolcation as different entities.
Is there any better solution ,or more generic structure which can help in this case?
#Entity
public class TwoFactorSettings{
private int id;
private String createdDate;
private String createdBy;
#OneToOne
private IpConfigs ipConfigs;
#OneToOne
private Geolcation geoLocation;
#OneToOne
Organisation organisation;
}
Since every user belongs to an organization, you could create a one-to-one relationship for user with organization entity. Then create a relationship between an organization and its settings (ipConfigs, and geolocation) entities
#Entity
public class User {
#OneToOne(targetEntity=Organization.class)
#JoinColumn(name="organization_id")
private Organization organization;
}
#Entity
public class Organization {
#OneToOne(targetEntity=IpConfigs.class)
#JoinColumn(name="ipconfig_id")
private IpConfigs ipConfigs;
#OneToOne(targetEntity=Geolcation.class)
#JoinColumn(name="geolocation_id")
private Geolcation geoLocation;
}
Related
I'm doing some research around cryptocurrencies and when getting to the technical aspects of it I found a problem that maybe its more common and someone already found a solution.
I have found a database with historic information by product and it has different tables for the different combinations but the structure of the table is the same.
I have design this DBO, nothing rocket science:
public class ProductHistoryDbo {
private long id;
private long startTime;
private long endTime;
private float low;
private float high;
private float open;
private float close;
private float volume;
}
And the database has one table per (exchange, currency_in, currency_to)
product_history_gdax_bch_btc
product_history_gdax_bch_eur
...
There are 12 tables with the same structure and one additional with all the other tables that you can find inside.
So my idea is to have only one Entity and Repository but dynamically change, if possible, from which table to retrieve the data in spring-boot in order to adapt if in the future new tables are added without the need of adding boilerplate code.
Final E2E is to have an admin page with a combobox with all the tuples which will do a request to this server and changes in the database will not imply a change in the backend code.
You can create a base class and then extend it with the only difference for each final class being the table name.
Base class:
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Lut implements BaseCrudEntity<Long> {
}
Subclasses:
package xxx.lut;
import javax.persistence.*;
#Entity
#Table(name = "rwx_gnrl_lut_dm")
public class LutDm extends Lut {
}
I have Question, Like and Hashtag entities. Also there is one to many relationship between Like and Question entities. I am using google cloud endpoints and my problem begins here. In my list method, I return 20 question as json. But for each question object in query I have to check if user is already liked the question and also fetch related hashtags that belongs to the question. How can I do the same operation by key only batch query. Otherwise, I do
ofy().load().type(Like.class)
.filter("questionRef =", questionKey)
.filter("accountRef =", accountKey).first().now();
for each object.
Like entity
#Entity
#Cache
public class Like {
#Id
#Getter
protected Long id;
#Index
#Load
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Ref<Account> accountRef;
#Index
#Load
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Ref<Question> questionRef;
#Index
#Getter
protected Date createdAt;
Like() {
}
Like(Key<Account> accountKey) {
this.accountRef = Ref.create(accountKey);
this.createdAt = new Date();
}
}
Hashtag entity
#Entity
#Cache
public class Hashtag implements Model<Hashtag> {
#Id
#Getter
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Long id;
#Index
#Load
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Ref<Question> questionRef;
#Index
#Getter
#Setter
private String text;
private Hashtag() {
}
private Hashtag(Builder builder) {
this.questionRef = builder.questionRef;
this.text = builder.text;
}
}
There are several parts to this question.
First, hashtags: Just store hashtags in the Question as an indexed list property. Easy.
Second, likes: There are a couple ways to do this efficiently.
One is to create a Like entity with a natural key of "account:question" (use the stringified websafe key). This way you can do a batch get by key for all the {user,question} tuples. Some will be absent, some will be present. Reasonably efficient if you're only concerned about 20 questions, especially if you #Cache the Like.
Another is to create a separate Relation Index Entity that tracks all the likes of a user and just load those up each time. You can put 5k items in any list property, which means you'll need to juggle multiple entities when a user likes more than 5k things. But it's easy to load them all up with a single ancestor query. The RIE will need to be #Parented by the User.
On a separate note - don't call fields thingRef. It's just a thing. The data in the database is just a key. You can interchange Ref<?>, Key<?>, and the native low-level Key. Type information doesn't belong in database names.
I am not sure if you can change the structure of your entities. If the answer is no, then there is no option other than the approach you have taken.
If yes, I would suggest structuring your Question to include the Like and Hashtag information as well.
#Entity
public class Question {
#Id
private long id;
private Set<Key<Account>> likedBy;
private List<String> hashtags;
}
For a question, you can retrieve all the information in one single query. Then collect all the Account keys and make another datastore query to retrieve all the people who have liked the question using keys as below:
Map<Key<Account>, Account> likedByAccounts = ofy().load().keys(accountKeys);
I'm using Objectify on Google's AppEngine.
I have the following Entity-Model:
#Entity
public class ChallengeEntity {
#Id
private Long id;
#Index
public List<ChallengeParticipant> participants;
}
The Participant (not an entity... should it be one?)
public class ChallengeParticipant {
#Load
public Ref<UserEntity> user;
// ... participant-specific attributes
}
And the User-Entity:
#Entity
public class UserEntity {
#Id
Long id;
#Index
public String email = "";
}
Now how would I find all challenges for a given user-email?
Something along:
ofy().load().type(ChallengeEntity.class).filter("participants.user.email", "test#local.foo")
I am willing to adapt my entity-model to GAE's needs... how may I support this query efficiently and keep a nice model?
Thanks alot
Assuming your list of ChallengeParticipant is reasonably bounded (a few hundred at most) and you aren't at risk of hitting the 1M per-entity size limit, you're probably best leaving it as embedded.
To perform your query, first lookup the person by email, then filter by person:
UserEntity user = // load user (or get the key) by email
ofy().load().type(ChallengeEntity.class).filter("participants.user", user);
Note that you need to #Index the ChallengeParticipant.user field, not the ChallengeEntity.participants list.
Assuming that email is unique for a user, I'd keep ChallengeParticipant as a separate entity and maintain 2 way relationship with ChallangeEntity:
public class ChallengeParticipant {
#Id
String email; // must be able to uniquely identify a user.
List<Ref<ChallengeEntity>> challenges;
// ... participant-specific attributes
}
ChallengeEntity will exist as is but without any #Index
#Entity
public class ChallengeEntity {
#Id
private Long id;
public List<Ref<ChallengeParticipant>> participants;
}
When you want to add a new participant to a challenge, update both entities (Participant & Challenge) in one transaction. As there are no indexes involved, you'll always get consistent results.
I am totally new at this, I am sorry if it is stupid question.
I am trying to design database model for Google App Engine in JPA, but I am unable to get it right. When I find the way I can't get annotations right or I am getting error about M:N not supported in Google App Engine.
I need entity user to have multiple groups and groups have multiple users and there are users who are also group admins.
My basic model was User -> usergroup(user; group; (bool)isAdmin) <-Group
Can somebody give a clean and simple example of how to define relationships?
Please try this.
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
private String name;
#ManyToOne(fetch = FetchType.LAZY)
private UserGroup usergroup;
}
class userGroup
#Entity
public class UserGroup {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
private String name;
private boolean admin;
#OneToMany(mappedBy = "usergroup", cascade = CascadeType.ALL)
private List<User> users = new ArrayList<User>();
}
please be noticed GAE have limitation on JPA you can read more here
I don't know anything about Google App Engine, but I can help with JPA though.
The problem here is the "isAdmin" column, which prevents the data model to be a simple #ManyToMany relationship with a joiner table.
With the introduction of this field, in the data model you need a Map on the User entity with key=Group and value=isAdmin, similarly you need a corresponding Map in the Group entity in order to know if each User is an admin.
This is modeled with #ElementCollection in the following way:
#Entity
#Table(name="User")
public class User
{
#Id
#GeneratedValue(strategy= GenerationType.TABLE)
private int id;
private String name;
#ElementCollection
#CollectionTable(name="Users_Groups", joinColumns={#JoinColumn(name="userId")})
#MapKeyJoinColumn(name="groupId")
#Column(name="isAdmin")
private Map<Group, Boolean> groups;
}
#Entity
#Table(name="Group")
public class Group
{
#Id
#GeneratedValue(strategy= GenerationType.TABLE)
private int id;
private String name;
#ElementCollection
#CollectionTable(name="Users_Groups", joinColumns={#JoinColumn(name="groupId")})
#MapKeyJoinColumn(name="userId", insertable=false, updatable=false)
#Column(name="isAdmin", insertable=false, updatable=false)
private Map<User, Boolean> users;
}
The important annotation is #ElementCollection, the other annotations are just to name the specific columns of the collection table and make sure they match from both entities: #CollectionTable gives the name of the table and the name of the column representing the id in the current entity. #MapKeyJoinColumn gives the name of the column representing the id of the "key" element in the Map, and #Column gives the name of the "value" element in the map.
I'm not sure if the insertable=false and updatable=false are needed in one of the entities, might avoid adding duplicate rows due to the cyclic dependency between User and Group.
Also you need to manually create the collection table, because at least EclipseLink tries to create it with two "groupId" and "isAdmin" columns. You might consider reviewing the design if it is absolutely needed a cyclic dependency between User and Group.
I'm getting into problems with JPA. I'm trying to implement a database that allows users to follow other users and be followed.
I think I'd need (summing up) something like this:
USER_TABLE: id | userName
RELATIONSHIP_TABLE: id | follower | followed | acceptation
I have two entities (also summed up):
#Entity
public class User implements Serializable {
#Id
private Long id;
private String userName;
#OneToMany
private Collection<Relationship> followings;
}
#Entity
public class Relationship implements Serializable {
#Id
private Long id;
private User follower;
private User followed;
private boolean accepted;
}
My problem is that I'm not sure if it's possible to do this, because I obtain more tables that the two that I need.
Can anybody help me?
Thanks and sorry about my english.
You obtain more tables because you did not make the associations bidirectional. JPA has no way to know that Relationship.follower is the other side of the User.followings if you don't tell:
#Entity
public class User implements Serializable {
#OneToMany(mappedBy = "follower")
private Collection<Relationship> followings;
// ...
}
#Entity
public class Relationship implements Serializable {
#ManyToOne
#JoinColumn(name = "follower")
private User follower;
#ManyToOne
#JoinColumn(name = "followed")
private User followed;
// ...
}
The documentation of course explains how that works.