title: How to integrate or make use of KeyCloak user database in my application?
body: <p>So far I have been playing with KeyCloak and been able to set it up and running the customer-portal example successfully. Now I need to actually use it in my application, and I am not totally sure whether KeyCloak is the right thing that I am looking for, but I believe my need is just a common use case and hopefully KeyCloak is the right software that I am looking for..</p>
  
  <p>When a user comes to my website, he registers and makes a post. Both the post and the user information is stored into databases, and the link between the user and post, i.e. who made which post? So I have two tables in my database: Post(id, post) and User(id,name), and another table UserPost(PostID, UserID) to store linking information. This is all fine in my own database.</p>
  
  <p>But now when KeyCloak comes into play, the user first registers in KeyCloak server and user information are stored in its own database there, which seems unrelated to the database (Post and User) in my application. I don't want to duplicate two User databases in two servers, right? Even if I can tolerate the duplication, how to make the connection between KeyCloak database and my application database? I am using JBoss, Hibernate/JPA in my application.</p>
  
  <p>Maybe I am missing something in the way how to connect KeyCloak user table with my own application database. Is there any tutorial or documentation that I can read? </p>
  
  <p>Thank you.</p>
  
  <p>Updated: This User table in my application only stores an id, which will come from the KeyCloak user registration information, and one more field 'reputation' which will be assigned based on this user's new post. Most other attributes of a user will be kept intact in the KeyCloak's USER_ENTITY table. Now, whenever a new user registers, KeyCloak will insert a record into the USER_ENTITY table. No worry about that. But at the same time, I need to add a record to the User table in my application based on the user ID from the KeyCloak USER_ENTITY. The question is how to get the User ID from Keycloak from the registration html page?  </p>
  
  <pre><code>@Entity
  public class User {
  
      @Id
      private Long id;
  
      private int reputation = 0;
  
      @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
       private List&lt;Post&gt; posts = new ArrayList&lt;&gt;();
  
       public User() {
  
       }
  
       public User(Long id, int reputation) {
           this.id = id;
           this.reputation = reputation;
       }
  
       public void setId(Long id) {
  
       }
  
       public Long getId() {
           return id;
       }
  
       public void setReputation(int reputation) {
           this.reputation = reputation;
       }
  
       public int getReputation() {
           return reputation;
       }
  
       public List&lt;Post&gt; getPosts() {
           return posts;
       }
  }
  
  
  @Entity
  public class Post {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @ManyToOne
      private User user;
  
      private String text;
  
      public Post() {
  
      }
  
      public Long getId() {
          return id;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public User getUser() {
          return user;
      }
  
      public void setText(String text) {
          this.text = text;
      }
  
      public String getText() {
          return text;
      }
  }
  </code></pre>