当表与OneToOne关系链接时,如何通过JPA在MySQL数据库上正确插入数据

时间:2014-06-10 12:03:59

标签: java mysql jpa entity

我有疑问,因此我不知道如何正确行事。我有三个不同的表,如下图所示。我正在使用JPA。以下是Netbeans自动生成的代码,用于显示实体。我的问题是我应该坚持进入数据库?如果我只放user,我会自动保留电话号码和客户端吗?我很困惑

这是一个代码

UserCredential user = new UserCredential();
        user.setUsername(username);
        user.setPassword(utilBean.hashPassword(password));
        user.setEmail(email);

        Client client = new Client();
        client.setUserCredential(user);
        client.setName(name);
        client.setSurname(surname);
        client.setAddress(address);
        client.setCity(city);
        client.setZipCode(zipCode);
        client.setCountry(country);
        client.setFidelityPoints(0);

        ClientPhoneNumbers phoneNumber = new ClientPhoneNumbers();

        phoneNumber.setUsername(username);

        if(homeNumber != null || !(homeNumber.equals("")))
        {
            phoneNumber.setCountryCodeHome(areaCodeHomeNumber);
            phoneNumber.setHome(homeNumber);
        }

        phoneNumber.setCountryCodeMobile(areaCodeMobileNumber);
        phoneNumber.setMobile(mobileNumber);

        client.setClientPhoneNumbers(phoneNumber);


        em.persist(user);

enter image description here

USER CREDENTIAL

@Entity
@Table(name = "user_credential")
@NamedQueries(
{
    @NamedQuery(name = "UserCredential.findAll", query = "SELECT u FROM UserCredential u")
})
public class UserCredential implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 16)
    @Column(name = "username")
    private String username;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "email")
    private String email;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "password")
    private String password;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "groupname")
    private String groupname;
    @Column(name = "create_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "userCredential")
    private Client client;

    public UserCredential()
    {
    }

    public UserCredential(String username)
    {
        this.username = username;
    }

    public UserCredential(String username, String email, String password, String groupname)
    {
        this.username = username;
        this.email = email;
        this.password = password;
        this.groupname = groupname;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getGroupname()
    {
        return groupname;
    }

    public void setGroupname(String groupname)
    {
        this.groupname = groupname;
    }

    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public Client getClient()
    {
        return client;
    }

    public void setClient(Client client)
    {
        this.client = client;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (username != null ? username.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserCredential))
        {
            return false;
        }
        UserCredential other = (UserCredential) object;
        if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "it.volaconoi.entity.UserCredential[ username=" + username + " ]";
    }

}

客户端

@Entity
@Table(name = "client")
@NamedQueries(
{
    @NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c")
})
public class Client implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 16)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "surname")
    private String surname;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "address")
    private String address;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "city")
    private String city;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "zip_code")
    private String zipCode;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "country")
    private String country;
    @Basic(optional = false)
    @NotNull
    @Column(name = "fidelity_points")
    private int fidelityPoints;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "client")
    private ClientPhoneNumbers clientPhoneNumbers;
    @JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false)
    @OneToOne(optional = false)
    private UserCredential userCredential;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "username")
    private Collection<Reservation> reservationCollection;

    public Client()
    {
    }

    public Client(String username)
    {
        this.username = username;
    }

    public Client(String username, String name, String surname, String address, String city, String zipCode, String country, int fidelityPoints)
    {
        this.username = username;
        this.name = name;
        this.surname = surname;
        this.address = address;
        this.city = city;
        this.zipCode = zipCode;
        this.country = country;
        this.fidelityPoints = fidelityPoints;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSurname()
    {
        return surname;
    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getZipCode()
    {
        return zipCode;
    }

    public void setZipCode(String zipCode)
    {
        this.zipCode = zipCode;
    }

    public String getCountry()
    {
        return country;
    }

    public void setCountry(String country)
    {
        this.country = country;
    }

    public int getFidelityPoints()
    {
        return fidelityPoints;
    }

    public void setFidelityPoints(int fidelityPoints)
    {
        this.fidelityPoints = fidelityPoints;
    }

    public ClientPhoneNumbers getClientPhoneNumbers()
    {
        return clientPhoneNumbers;
    }

    public void setClientPhoneNumbers(ClientPhoneNumbers clientPhoneNumbers)
    {
        this.clientPhoneNumbers = clientPhoneNumbers;
    }

    public UserCredential getUserCredential()
    {
        return userCredential;
    }

    public void setUserCredential(UserCredential userCredential)
    {
        this.userCredential = userCredential;
    }

    public Collection<Reservation> getReservationCollection()
    {
        return reservationCollection;
    }

    public void setReservationCollection(Collection<Reservation> reservationCollection)
    {
        this.reservationCollection = reservationCollection;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (username != null ? username.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Client))
        {
            return false;
        }
        Client other = (Client) object;
        if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "it.volaconoi.entity.Client[ username=" + username + " ]";
    }

}

**电话号码**

@Entity
@Table(name = "client_phone_numbers")
@NamedQueries(
{
    @NamedQuery(name = "ClientPhoneNumbers.findAll", query = "SELECT c FROM ClientPhoneNumbers c")
})
public class ClientPhoneNumbers implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 16)
    @Column(name = "username")
    private String username;
    @Size(max = 8)
    @Column(name = "country_code_home")
    private String countryCodeHome;
    @Size(max = 45)
    @Column(name = "home")
    private String home;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    @Column(name = "country_code_mobile")
    private String countryCodeMobile;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "mobile")
    private String mobile;
    @JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false)
    @OneToOne(optional = false)
    private Client client;

    public ClientPhoneNumbers()
    {
    }

    public ClientPhoneNumbers(String username)
    {
        this.username = username;
    }

    public ClientPhoneNumbers(String username, String countryCodeMobile, String mobile)
    {
        this.username = username;
        this.countryCodeMobile = countryCodeMobile;
        this.mobile = mobile;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getCountryCodeHome()
    {
        return countryCodeHome;
    }

    public void setCountryCodeHome(String countryCodeHome)
    {
        this.countryCodeHome = countryCodeHome;
    }

    public String getHome()
    {
        return home;
    }

    public void setHome(String home)
    {
        this.home = home;
    }

    public String getCountryCodeMobile()
    {
        return countryCodeMobile;
    }

    public void setCountryCodeMobile(String countryCodeMobile)
    {
        this.countryCodeMobile = countryCodeMobile;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }

    public Client getClient()
    {
        return client;
    }

    public void setClient(Client client)
    {
        this.client = client;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (username != null ? username.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ClientPhoneNumbers))
        {
            return false;
        }
        ClientPhoneNumbers other = (ClientPhoneNumbers) object;
        if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "it.volaconoi.entity.ClientPhoneNumbers[ username=" + username + " ]";
    }

}

1 个答案:

答案 0 :(得分:0)

如果你想同时保留client,你必须在调用persist之前调用user.setClient(client)