英文:
How to create two entities using springboot microservices
问题
我正在使用微服务架构,尝试创建一个名为userApplication的实体,该实体仅包含用于凭据目的所需的所有数据,这意味着关于我的用户的基本信息。此外,我还有另一个名为userData的实体,其中包含我当前应用程序所需的所有数据,因此我不太确定如何将这两者进行匹配,并且在创建userApplication实体时如何调用userData服务,因为每个实体都有自己的微服务。
我尝试使用Feign,但我不确定如何操作。
// ApplicationUser
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "BINARY(16)")
private UUID id;
@Column(unique = true, length = 20)
private String username;
private String normalizedUsername;
@Column(length = 60)
private String password;
private Boolean enabled;
private String firstname;
private String lastname;
@Column(unique = true, length = 100)
private String email;
private Boolean emailConfirmed;
private String normalizedEmail;
private String phonenumber;
private Boolean phonenumberConfirmed;
private Boolean twoFactorEnabled;
@OneToMany(mappedBy = "user")
@Column(name = "user_datum", table = "user_data")
private Collection<UserData> userDatum;
// UserData
private String email;
private String firstname;
private String lastname;
private String address;
private LocalDate birthday;
private Double height;
private Double weight;
private String bloodType;
private String city;
private String country;
@ManyToOne()
@PrimaryKeyJoinColumn(name = "id")
private ApplicationUser user;
// repository
@RepositoryRestResource(path = "users")
public interface UserRepository extends JpaRepository<ApplicationUser, UUID> {
public ApplicationUser findByUsername(String username);
public ApplicationUser CreateUserWithData();
}
英文:
I'm using a microservices architecture, and I'm trying to create a userApplication entity which has all data necessary for a credential purpose only, that means basic info about my user, also i have another entity userData which have all the data for my current application, so i dont really sure how to match those two and also call userData service when i create a userApplication entity because each one has his own microservice
I tried to use Feign but I'm not sure how to do it.
//ApplicationUser
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "BINARY(16)")
private UUID id;
@Column(unique = true, length = 20)
private String username;
private String normalizedUsername;
@Column(length = 60)
private String password;
private Boolean enabled;
private String firstname;
private String lastname;
@Column(unique = true, length = 100)
private String email;
private Boolean emailConfirmed;
private String normalizedEmail;
private String phonenumber;
private Boolean phonenumberConfirmed;
private Boolean twoFactorEnabled;
@OneToMany(mappedBy = "id")
@Column(name = "user_datum", table = "user_data")
private Collection<UserData> userDatum;
//UserData
private String email;
private String firstname;
private String lastname;
private String address;
private LocalDate birthday;
private Double height;
private Double weight;
private String bloodType;
private String city;
private String country;
@ManyToOne()
@PrimaryKeyJoinColumn(name = "id")
private ApplicationUser user;
//repository
@RepositoryRestResource(path = "users")
public interface UserRepository extends JpaRepository<ApplicationUser, UUID> {
public ApplicationUser findByUsername(String username);
public ApplicationUser CreateUserWithData ();
}
答案1
得分: 0
根据我的经验,你应该将UserData类导入userApplication中。然后使用Feign调用userData服务来获取UserData数据。在完成这些操作之后,你可以使用Apache BeanUtils将UserData对象复制到ApplicationUser对象中。
英文:
In my experience, you should import UserData class into userApplication. Then use feign to invoke userData service to get UserData datas. After doing it, you can use apache BeanUtils to copy UserData obj to ApplicationUser obj.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论