如何使用Spring Boot微服务创建两个实体。

huangapple go评论121阅读模式
英文:

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 = &quot;UUID&quot;)
    @GenericGenerator(name = &quot;UUID&quot;, strategy = &quot;org.hibernate.id.UUIDGenerator&quot;)
    @Column(name = &quot;id&quot;, updatable = false, nullable = false, columnDefinition = &quot;BINARY(16)&quot;)
    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 = &quot;id&quot;)
    @Column(name = &quot;user_datum&quot;, table = &quot;user_data&quot;)
    private Collection&lt;UserData&gt; 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 = &quot;id&quot;)
    private ApplicationUser user;

//repository

    @RepositoryRestResource(path = &quot;users&quot;)
public interface UserRepository extends JpaRepository&lt;ApplicationUser, UUID&gt; {

    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.

huangapple
  • 本文由 发表于 2020年10月28日 05:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64563033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定