SpringBoot + JPA + Hibernate关系映射

时间:2016-05-20 11:54:50

标签: mysql spring hibernate jpa spring-data

我有一种情况,我必须在多个表中注册用户和信息存储。

表格结构:

Users table

Users Table

UserProfiles Table

UserProfiles Table

Configurations Table

Configurations Table

Address table

Addresses Table

因为,在snap上面显示了我的表结构,我想用hibernate和JPA将所有这些表映射到springboot。我真的不知道如何保持这些表之间的关系。

**Explanation**
  1. 一个用户只能映射到一个配置文件。
  2. 一个个人资料可以包含多个地址
  3. 将国家,州和城市地图映射到配置表configid,例如country = configid,state = configid,city = configid。
  4. Configurations表是address的基表,users表是userprofiles的基表,userprofile是地址的基表。
  5. 请建议我的解决方案。如果需要进一步澄清,请告诉我。

    更新#1

    User.java

    @Entity
    @Table(name = "users")
    @PasswordMatches
    public class User  {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "user_id")
        private long userId;
    
        @NotNull
        @NotEmpty
        @Size(min=5)
        @Column(name = "username")
        private String userName;
    
        @NotNull
        @NotEmpty
        @ValidEmail
        @Size(min=5)
        private String email;
    
        @NotNull
        @NotEmpty
        @Size(min=6, max=20)
        private String password;
    
        @NotNull
        @NotEmpty
        @Size(min=8, max=20)
        private transient String cpassword;
      .... // getter & setters and default constructor
    }
    

    UserProfile.java

    public class UserProfile {
    
    
        @NotNull
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long userProfileId;
    
        @OneToOne
        private User user;
    
        @NotNull
        @NotEmpty
        private String firstName;
    
        @NotNull
        @NotEmpty
        private String lastName;
    
        @NotNull
        @NotEmpty
        private String gender;
    
        @NotNull
        @NotEmpty
        private String phoneNumber;
    
     .... // getter & setter and default constructor.
    }
    

    Address.java

    public class Address {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long addressId;
    
        @NotNull
        @NotEmpty
        @Size(min=5)
        private String address;
    
        @NotNull
        @NotEmpty
        @OneToOne
        private Configuration country;
    
        @NotNull
        @NotEmpty
        @OneToOne
        private Configuration state;
    
        @NotNull
        @NotEmpty
        @OneToOne
        private Configuration city;
    
        @NotNull
        @NotEmpty
        @Size(min=6, max=6)
        private int pincode;
    
        @ManyToOne
        private UserProfile userProfile;
        ... // getters & setter and default method
    }
    

    Configuration.java

    @Entity
    @Table(name = "configurations")
    @EntityListeners(AuditingEntityListener.class)
    public class Configuration {
    
        @Id
        @Column(name="configid")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long configId;
    
        @NotNull
        @NotEmpty
        @Size(min=3)
        @Column(name="configname")
        private String configName;
    
        @NotNull
        @NotEmpty
        @Size(min=3)
        @Column(name="configtype")
        private String configType;
    
        @ManyToOne
        @JoinColumn(name="parentid")
        private Configuration parentid;
    
        @OneToMany(mappedBy="parentid")
        public List<Configuration> childConfigs = new ArrayList<Configuration>();
    
        @CreatedDate
        @Column(name = "created_at")
        private long createdDate;
    
        @Column(name = "created_by")
        @CreatedBy
        private String createdBy;
    
        @LastModifiedDate
        @Column(name = "updated_at")
        private long modifiedDate;
    
        @Column(name = "updated_by")
        @LastModifiedBy
        private String modifiedBy;
       .... //getters & setters and default constructor
    }
    

    register.html

    <form action="#" th:action="@{'/signup/create'}"
                                    th:object="${user}" method="post" autocomplete="off">
                                    <fieldset>
                                        <div class="form-group">
                                            <label th:text="#{register.label.username}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="text" th:field="*{userName}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('userName')}"
                                                    th:errors="*{userName}">User Name Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.firstname}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="text" th:field="*{firstName}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('firstName')}"
                                                    th:errors="*{firstName}">First Name Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.lastname}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="text" th:field="*{lastName}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('lastName')}"
                                                    th:errors="*{lastName}">Last Name Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.gender}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="text" th:field="*{gender}" class="form-control" />
                                                <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('gender')}" th:errors="*{gender}">Gender
                                                        Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.phonenumber}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="text" th:field="*{phoneNumber}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('phoneNumber')}"
                                                    th:errors="*{phoneNumber}">Phone Number Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.country}"
                                                class="col-md-4 control-label">Country:</label>
                                            <div class="col-md-6">
                                                <select class="form-control" th:field="*{country}">
                                                    <option value="-1">--- Select Country ---</option>
                                                    <option th:each="country : ${countries}" th:value="${country.configid}" th:text="${coutry.configname}"></option>
                                                </select> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('country')}"
                                                    th:errors="*{country}">Country Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.state}"
                                                class="col-md-4 control-label">State:</label>
                                            <div class="col-md-6">
                                                <select class="form-control" th:field="*{state}">
                                                    <option value="-1">--- Select State ---</option>
                                                    <option value="2">Delhi</option>
                                                    <option value="3">Utter Pradesh</option>
                                                </select> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('state')}" th:errors="*{country}">Country
                                                        Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.city}"
                                                class="col-md-4 control-label">City:</label>
                                            <div class="col-md-6">
                                                <select class="form-control" th:field="*{city}">
                                                    <option value="-1">--- Select City ---</option>
                                                    <option value="4">Delhi</option>
                                                    <option value="5">Kanpur</option>
                                                    <option value="3">Noida</option>
                                                </select> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('city')}" th:errors="*{country}">Country
                                                        Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.pincode}"
                                                class="control-label col-md-4">Pin Code</label>
                                            <div class="col-md-6">
                                                <input type="text" th:field="*{pincode}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('pincode')}"
                                                    th:errors="*{pincode}">Pincode Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{register.label.address}"
                                                class="control-label col-md-4">Address:</label>
                                            <div class="col-md-6">
                                                <textarea th:field="*{address}" class="form-control"></textarea>
                                                <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('address')}"
                                                    th:errors="*{address}">Address Error</strong></span>
                                            </div>
                                        </div>
                                    </fieldset>
                                    <fieldset>
                                        <div class="form-group">
                                            <label th:text="#{login.label.email}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="email" th:field="*{email}" class="form-control" />
                                                <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Email
                                                        Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{login.label.password}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="password" th:field="*{password}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('password')}"
                                                    th:errors="*{password}">Password Error</strong></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label th:text="#{login.label.cpassword}"
                                                class="control-label col-md-4" />
                                            <div class="col-md-6">
                                                <input type="password" th:field="*{cpassword}"
                                                    class="form-control" /> <span class="help-block"> <strong
                                                    th:if="${#fields.hasErrors('cpassword')}"
                                                    th:errors="*{cpassword}">Confirm Password Error</strong></span>
                                            </div>
                                        </div>
                                    </fieldset>
                                    <div class="form-group">
                                        <div class="col-md-6 col-md-offset-4">
                                            <button type="submit" class="btn btn-primary"
                                                th:utext="register.submitbtn.label">
                                                <i class="fa fa-btn fa-sign-in"></i> Register Me
                                            </button>
                                            <a class="btn btn-link" href="@{/login}"
                                                th:text="#{register.link.signin}">Registered ! Sign In</a>
                                        </div>
                                    </div>
                                </form>
    

    我在尝试创建新用户时获得属性不可读错误。

    请查看并建议我的解决方案。 感谢。

0 个答案:

没有答案