hibernate annotation 主键生成策略问题:
  我有一个表,主键是id varchar(32) <pk,fk>
  我应该怎样设置annotation,让id既是uuid的生成策略,又是外键了.(与外表是多对一关联).
 最好有例子啊!!谢谢啊

解决方案 »

  1.   

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.mmc.bean.Brand" table="brand" schema="dbo" catalog="computerdb" lazy="false">
            <id name="bid" type="java.lang.String">
                <column name="bid" length="32" />
                <generator class="uuid.hex" />
            </id>
            <property name="bname" type="java.lang.String">
                <column name="bname" length="30"  />
            </property>
            <set name="montherboards" inverse="true">
                <key>
                    <column name="brand_id" length="32"  />
                </key>
                <one-to-many class="com.mmc.bean.Montherboard" />
            </set>
            <set name="models" inverse="true">
                <key>
                    <column name="bid" length="32" />
                </key>
                <one-to-many class="com.mmc.bean.Model" />
            </set>
            <set name="monitors" inverse="true">
                <key>
                    <column name="brand_id" length="32" />
                </key>
                <one-to-many class="com.mmc.bean.Monitor" />
            </set>
            <set name="cpus" inverse="true">
                <key>
                    <column name="brand_id" length="32" />
                </key>
                <one-to-many class="com.mmc.bean.Cpu" />
            </set>
            <set name="memories" inverse="true">
                <key>
                    <column name="brand_id" length="32" />
                </key>
                <one-to-many class="com.mmc.bean.Memory" />
            </set>
            <set name="keyboards" inverse="true">
                <key>
                    <column name="brand_id" length="32"  />
                </key>
                <one-to-many class="com.mmc.bean.Keyboard" />
            </set>
            <set name="videos" inverse="true">
                <key>
                    <column name="brand_id" length="32"  />
                </key>
                <one-to-many class="com.mmc.bean.Video" />
            </set>
            <set name="mouses" inverse="true">
                <key>
                    <column name="brand_id" length="32"  />
                </key>
                <one-to-many class="com.mmc.bean.Mouse" />
            </set>
            <set name="harddisks" inverse="true">
                <key>
                    <column name="brand_id" length="32"  />
                </key>
                <one-to-many class="com.mmc.bean.Harddisk" />
            </set>
        </class>
    </hibernate-mapping>
      

  2.   

    package com.mmc.bean;import java.util.HashSet;
    import java.util.Set;/**
     * Brand generated by MyEclipse Persistence Tools
     */public class Brand implements java.io.Serializable { // Fields private String bid; private String bname; private Set montherboards = new HashSet(0); private Set models = new HashSet(0); private Set monitors = new HashSet(0); private Set cpus = new HashSet(0); private Set memories = new HashSet(0); private Set keyboards = new HashSet(0); private Set videos = new HashSet(0); private Set mouses = new HashSet(0); private Set harddisks = new HashSet(0); // Constructors /** default constructor */
    public Brand() {
    } /** minimal constructor */
    public Brand(String bid, String bname) {
    this.bid = bid;
    this.bname = bname;
    } /** full constructor */
    public Brand(String bid, String bname, Set montherboards, Set models,
    Set monitors, Set cpus, Set memories, Set keyboards, Set videos,
    Set mouses, Set harddisks) {
    this.bid = bid;
    this.bname = bname;
    this.montherboards = montherboards;
    this.models = models;
    this.monitors = monitors;
    this.cpus = cpus;
    this.memories = memories;
    this.keyboards = keyboards;
    this.videos = videos;
    this.mouses = mouses;
    this.harddisks = harddisks;
    } // Property accessors public String getBid() {
    return this.bid;
    } public void setBid(String bid) {
    this.bid = bid;
    } public String getBname() {
    return this.bname;
    } public void setBname(String bname) {
    this.bname = bname;
    } public Set getMontherboards() {
    return this.montherboards;
    } public void setMontherboards(Set montherboards) {
    this.montherboards = montherboards;
    } public Set getModels() {
    return this.models;
    } public void setModels(Set models) {
    this.models = models;
    } public Set getMonitors() {
    return this.monitors;
    } public void setMonitors(Set monitors) {
    this.monitors = monitors;
    } public Set getCpus() {
    return this.cpus;
    } public void setCpus(Set cpus) {
    this.cpus = cpus;
    } public Set getMemories() {
    return this.memories;
    } public void setMemories(Set memories) {
    this.memories = memories;
    } public Set getKeyboards() {
    return this.keyboards;
    } public void setKeyboards(Set keyboards) {
    this.keyboards = keyboards;
    } public Set getVideos() {
    return this.videos;
    } public void setVideos(Set videos) {
    this.videos = videos;
    } public Set getMouses() {
    return this.mouses;
    } public void setMouses(Set mouses) {
    this.mouses = mouses;
    } public Set getHarddisks() {
    return this.harddisks;
    } public void setHarddisks(Set harddisks) {
    this.harddisks = harddisks;
    }}
      

  3.   

    package com.mmc.bean;import java.util.HashSet;
    import java.util.Set;public class Montherboard implements java.io.Serializable { private String boardId; private Brand brand; private String chipset; private Set configures = new HashSet(0); public Montherboard() {
    } public Montherboard(String boardId, Brand brand, String chipset) {
    this.boardId = boardId;
    this.brand = brand;
    this.chipset = chipset;
    } /** full constructor */
    public Montherboard(String boardId, Brand brand, String chipset,
    Set configures) {
    this.boardId = boardId;
    this.brand = brand;
    this.chipset = chipset;
    this.configures = configures;
    } // Property accessors public String getBoardId() {
    return this.boardId;
    } public void setBoardId(String boardId) {
    this.boardId = boardId;
    } public Brand getBrand() {
    return this.brand;
    } public void setBrand(Brand brand) {
    this.brand = brand;
    } public String getChipset() {
    return this.chipset;
    } public void setChipset(String chipset) {
    this.chipset = chipset;
    } public Set getConfigures() {
    return this.configures;
    } public void setConfigures(Set configures) {
    this.configures = configures;
    }}
      

  4.   

    我要的是hibernate annotation,不要用配置文件(xml)啊
      

  5.   

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.mmc.bean.Montherboard" table="montherboard" schema="dbo" catalog="computerdb" lazy="false">
            <id name="boardId" type="java.lang.String">
                <column name="board_id" length="32" />
                <generator class="uuid.hex" />
            </id>
            <many-to-one name="brand" class="com.mmc.bean.Brand" fetch="select">
                <column name="brand_id" length="32" not-null="true" />
            </many-to-one>
            <property name="chipset" type="java.lang.String">
                <column name="chipset" length="20" not-null="true" />
            </property>
            <set name="configures" inverse="true">
                <key>
                    <column name="board_id" length="32" not-null="true" />
                </key>
                <one-to-many class="com.mmc.bean.Configure" />
            </set>
        </class>
    </hibernate-mapping>---是否应该感谢我一下,我还是楼上的,由于csdn的限定,不能回复过三,所以就用了这个公共帐号.