没有主键的表,hibernate怎么去update

有一个表,没有设置主键,又想update,怎么办?

 

发现这个表其实是可以有复合主键的,但是database里没有设置,而hibernate写annotation的时候又一定要设置主键@Id,如果你只设置一个,在update的时候会报错,因为一个键不是唯一,但是设置两个,hibernate又不允许。

用嵌套吧。

 



import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@Entity
@Table(name = "tbl_user_download_buy_list")
public class UserDownloadBuyListVO implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private DownloadbuyListPK dpk = new DownloadbuyListPK();

    private Long downloadStatusID;
    private String downloadAuthKey;
    private String EncDRMKey;
    private Long subscriptionID;
   

    @Column(name = "DOWNLOAD_STATUS_ID", nullable = false, length = 20)
    public Long getDownloadStatusID() {
        return downloadStatusID;
    }

    public void setDownloadStatusID(Long downloadStatusID) {
        this.downloadStatusID = downloadStatusID;
    }

    @Column(name = "DOWNLOAD_AUTH_KEY", nullable = false, length = 32)
    public String getDownloadAuthKey() {
        return downloadAuthKey;
    }

    public void setDownloadAuthKey(String downloadAuthKey) {
        this.downloadAuthKey = downloadAuthKey;
    }

    @Column(name = "ENCRYPTED_DRM_KEY", nullable = false, length = 45)
    public String getEncDRMKey() {
        return EncDRMKey;
    }

    public void setEncDRMKey(String encDRMKey) {
        EncDRMKey = encDRMKey;
    }

    @Column(name = "SUBSCRIPTION_ID", nullable = true, length = 45)
    public Long getSubscriptionID() {
        return subscriptionID;
    }

    public void setSubscriptionID(Long subscriptionID) {
        this.subscriptionID = subscriptionID;
    }



    @EmbeddedId
    public DownloadbuyListPK getDpk() {
        return dpk;
    }

    public void setDpk(DownloadbuyListPK dpk) {
        this.dpk = dpk;
    }




    @Embeddable
    public static class DownloadbuyListPK implements Serializable {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public DownloadbuyListPK() {
            super();
        }

        private String userUID;
        private String publishedPackageUID;
       
        @Column(name = "USER_UID", nullable = false, length = 32)
        public String getUserUID() {
            return userUID;
        }

        public void setUserUID(String userUID) {
            this.userUID = userUID;
        }

        @Column(name = "PUBLICATION_PACKAGE_UID", nullable = false, length = 32)
        public String getPublishedPackageUID() {
            return publishedPackageUID;
        }

        public void setPublishedPackageUID(String publishedPackageUID) {
            this.publishedPackageUID = publishedPackageUID;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result ;
            result = prime
                    * result
                    + ((publishedPackageUID == null) ? 0 : publishedPackageUID
                            .hashCode());
            result = prime * result
                    + ((userUID == null) ? 0 : userUID.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            DownloadbuyListPK other = (DownloadbuyListPK) obj;
            if (publishedPackageUID == null) {
                if (other.publishedPackageUID != null)
                    return false;
            } else if (!publishedPackageUID.equals(other.publishedPackageUID))
                return false;
            if (userUID == null) {
                if (other.userUID != null)
                    return false;
            } else if (!userUID.equals(other.userUID))
                return false;
            return true;
        }

       

    }

}

你可能感兴趣的:(没有主键的表,hibernate怎么去update)