Thrift file how to define a list of object id - go

I am using thrift + Go, and my thrift file looks like this:
namespace go user
struct Role {
1: string id;
2: string roleName;
}
struct User {
1: required string id;
2: required string email;
3: required string password;
4: required string name;
5: optional list<Role.id> roles;
}
when I run : thrift -r --gen go user.thrift
I got:
Type "Role.id" not defined
Any ideas on how I should achieve this?

Role.id is not a data type
struct User {
1: required string id;
2: required string email;
3: required string password;
4: required string name;
5: optional list<string> roles;
}
Side notes:
set<string> might be a better choice for roles ids
regarding required: I strongly suggest to read this comprehensive summary first, which has a whole section about that topic.

Related

How to include a thrift file from a different micro service

I am new to micro service and thrift world, I wonder how to include a thrift file from a different micro service.
ex:
In my user micro service, I have
namespace go user
struct User {
1: required string id;
2: required string email;
3: required string password;
4: optional list<Order> roles;
}
and in my order micro service, I have
namespace go order
struct Order {
1: required string id;
2: required string orderNumber;
}
if I also want my User struct to have a list of Order, how do I include it from my order micro service?
Thanks
You need to include the file, the reference the type with the included prefix, like so:
namespace go user
include "order.thrift"
struct User {
1: required string id;
2: required string email;
3: required string password;
4: optional list<order.Order> roles;
}
Recommended read: https://thrift.apache.org/docs/idl.html, escpecially this example

Bulk data to find exists or not : Spring Data JPA

I get an Post request that would give me a List<PersonApi> Objects
class PersonApi {
private String name;
private String age;
private String pincode ;
}
And I have an Entity Object named Person
#Entity
#Table(name = "person_master")
public class Person{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
#Column(name = "name")
String name;
#Column(name = "age")
String age;
#Column(name = "pincode ")
String pincode ;
}
My record from Post request would look something like this (pseudocode representation of the data below)
[
"Arun","33","09876gh"
"James","34","8765468"
]
I need to do a bulk-validation using Spring JPA.. Give the List<PersonApi> and get a True or False based on the condition that all the entries in the PersonApi objects list should be there in the database.
How to do this ?
The selected answer is not a right one. (not always right)
You are selecting the whole database to check for existence. Unless your use case is very special, i.e. table is very small, this will kill the performance.
The proper way may start from issuing repository.existsById(id) for each Person, if you never delete the persons, you can even apply some caching on top of it.
exists
Pseudo Code:
List<PersonApi> personsApiList = ...; //from request
List<Person> result = personRepository.findAll();
in your service class you can access your repository to fetch all database entities and check if your list of personapi's is completeley available.
boolean allEntriesExist = result.stream().allMatch(person -> personsApiList.contains(createPersonApiFromPerson(person)));
public PersonApi createPersonApiFromPerson(Person person){
return new PersonApi(person.getName(), person.getAge(), person.getPincode());
}

Spring Data Rest save record with relation (foreign-key) in Java (repo.save())

I don't know how to save a record in SDR with a link to an existing table.
For example:
There is a lookup-table Flag and another table Account with name and n:1 relation to Flag-ID.
The IDs in Flag-table are already created.
#Entity
public class Account{
public Account(String name, Flag flag){
this.name = name;
this.flag = flag;
}
#Id
private int id;
#Column
private String name;
#ManyToOne
private Flag flag;
// Getter & Setter
}
#Entity
public class Flag{
public Flag(String title){
this.title = title;
}
#Id
private int id;
#Column
private String title;
// Getter & Setter
}
Now I want to add an account and link it to the flag-id like this:
AccountRepo accountRepo;
accountRepo.save(new Account("Name", 0));
But I declared an object in my account-function and if I want to execute the save-function, I have to add a flag-object like this:
accountRepo.save(new Account("Name", new Flag("title")));
However in this case, the framework will add a new Flag-record, what I don't want. I only want to link it.
So I need help for solving my problem.
Thanks!
Edit:
The two answers from #piotr-sołtysiak and #upesh-m helped and worked for me. Thanks for your help!
You can use 'merge' of hibernate, ie. entityManager.merge(new Account("Name", new Flag("title"))). But the Id of the Flag should be an existing Id , so that it just adds an entry to Account.
ie. If you already have a flag record existing in db with id = 1, and you want to add an account linked to this flag, then use entityManager.merge(new Account("Name", existingFlagObject)
Find desired flag entity using dedicated repository, e.g
Flag flag = flagRespository.findByTitle("title");
Set it in Account entity and save:
accountRepo.save(new Account("Name", flag));

Variable 'this.userInfo' is unbound and cannot be determined

I am developing a maven JDO project, but I am getting this error when I am trying to make relation between two tables (user_login, user_role)
User_Login: user_id(primary key), user_name, user_password,user_role_id
User_Role: id(primary key), role
user_role_id is same as id of user_role table
User.java:
#PersistenceCapable(table = "user_login")
public class User {
#PrimaryKey
#Column(name="user_id")
private Integer userId=0;
#Column(name="user_profile_name")
private String userProfileName=null;
#Column(name="user_email")
private String userEmail=null;
#Column(name="user_contact")
private String userContact=null;
#Column(name="user_name")
private String userName=null;
#Column(name="user_password")
private String userPassword=null;
#ManyToOne
#Column(name="user_role_id")
private Integer userRoleId=0;
Role.java:
#PersistenceCapable(table = "user_role")
public class Role {
#PrimaryKey
#Column(name="id")
private Integer id=0;
#Column(name="role")
private String role=null;
#OneToMany
private User userInfo=null;
DAOImpol:
public List<Role> getUser(String username, String userpassword) {
PersistenceManager pm = this.pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
JDOPersistenceManager jdopm = (JDOPersistenceManager)pm;
try {
// Start the transaction
tx.begin();
TypesafeQuery<User> tq = jdopm.newTypesafeQuery(User.class);
//QUser user = QUser.candidate();
QRole role = QRole.candidate();
QUser userInfo=role.userInfo;
List<Role> result = tq.filter(userInfo.userName.eq(username).and(userInfo.userPassword.eq(userpassword))).executeList();
//result = tq.executeResultList(true, user.userId);
if(result.size()>0){
log.info(">>>>>00000000"+" "+result.get(0).getUser().getUserEmail());
log.info(">>>>>11111111"+" "+result.get(0).getRoleId()+" "+result.get(0).getRole());
}else{
log.info("<<<<<<<=====000000");
}
// Commit the transaction, flushing the object to the datastore
tx.commit();
return result;
}
finally {
if (tx.isActive())
{
// Error occurred so rollback the transaction
tx.rollback();
}
pm.close();
}
I am getting this error:
javax.jdo.JDOUserException: Variable 'this.userInfo' is unbound and
cannot be determined (is it a misspelled field name? or is not intended
to be a variable?)
NestedThrowables:
org.datanucleus.exceptions.NucleusUserException: Variable
'this.userInfo' is unbound and cannot be determined (is it a
misspelled
field name? or is not intended to be a variable?)
I found that you'll get this error from JDO if you're using progaurd and progaurd renames your private fields. Adding a -keep to the progaurd config to keep the package with your Persistence Capable classes will fix it.
For example, if you keep all of your Persistence Capable classes in com.example.server.orm package you'd add this to progaurd.conf
-keep class com.example.server.orm.** {*;}

Cannot implicitly convert type 'string' to Model

I keep getting these errors:
Error 1 Cannot implicitly convert type 'string' to 'NSWebSite.Models.Role'
public Role GetRoleForUser (User user)
{
if (!UserExists(user))
throw new ArgumentException(MissingUser);
return user.Roles.TargetRoleName;
}
Error 2 'NSWebSite.Models.User' does not contain a definition for 'RoleID'
User newUser = new User()
{
Name = name,
Password = FormsAuthentication.HashPasswordForStoringInConfigFile(
password.Trim(), "md5"),
Email = email,
RoleID = role.Id
};
Error 1 - I have no idea how to fix, so any advice is welcome.
Error 2 - I am getting it because my user model does not contain the definition for RoleID. If I want to set the role when I create a new user, what should I put here instead?
Below is my repository file (modified from here: brianleggDOTcom/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx) - DOT=. (dam antispam measures :-) )
http://www.mediafire.com/?gey4y9ub0v2u9nh
and my Model.Designer.cs file
http://www.mediafire.com/?qa3p9we8uqwfj09
Your Error #2 simply means that your User class has no definition for a RoleID property. Look at the class definition and you can define it there.
Your Error #1 looks like you have a method with a return type of Role, and you're trying to do:
return user.Roles.TargetRoleName; // this is a string???
If that string is what you do want to return, you'll need to modify your method like so:
string YourMethodName(...any parameters you have...)
{
// ...your code here
return SomeRole;
}
Or you can change your TargetRoleName property from type string to Role.
My first question would be: Why is this all in the same class?
I would start by sorting the classes: Role, User, etc.
You need to define what a Role is and what a User is, separately.
public class User {
private String name;,
private String password;
private String email;
private RoleID role;
public User(String name, String password, String email, RoleID role) {
this.name = name;
this.password = password;
this.email = email;
this.role = role;
}
public void someOtherMethod() {
//some code here, etc
}
}
I am kinda new to this part of MVC and C#, from the original example at: https://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx
I had changed the DB schema from a 1 to many for user and roles to a many to many relationship. Changing the DB scheme back and updating the entity schema made everything work again like from the original blog post.
Why the original poster did not separate the two classes I am not sure, but once I get things working like I want I will start looking at cleaning up the code. If some one has any suggestions about how to make the example from the above mentioned webpage work with a many to many relationship for the User and Roles, they will be greatly appreciated :-)

Resources