0

艰难我看到很多匹配的建议问题,这些建议似乎都没有回答我的问题。我是 Hibernate 的新手,刚开始使用注释。对 XML 配置稍微满意。我正在经历“利用休眠”并尝试注释示例。当尝试运行它时,我遇到了这个异常。我的代码在这里

休眠.cfg.xml

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>

` <session-factory>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!--  MySQL connection     -->
      <property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest</property>
      <property name="connection.username">root</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.password">root</property>`

    <property name="current_session_context_class">thread</property>
    <!-- this will show us all sql statements -->

    <property name="connection.pool_size">1</property>
    <property name="hibernate.show_sql">false</property>
    <!-- this will create the database tables for us --><!--
    <property name="hibernate.hbm2ddl.auto">create</property>
    --><!--<property name="jdbc.batch_size">0</property>
    <mapping resource="com/home/hibernate/data/Track.hbm.xml" />
    <mapping resource="com/home/hibernate/data/Artist.hbm.xml" />
    <mapping resource="com/home/hibernate/data/Album.hbm.xml" />
  -->
    <mapping class="com.home.hibernate.bean.Track"/>
    <mapping class="com.home.hibernate.bean.Album"/>
    <mapping class="com.home.hibernate.bean.AlbumTrack"/>
    <mapping class="com.home.hibernate.bean.Artist"/> 
  </session-factory>

</hibernate-configuration>

轨道.java

    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    import org.hibernate.annotations.CollectionOfElements;
    import org.hibernate.annotations.Index;

    @Entity
    @Table(name="TRACK")
    @NamedQueries({
        @NamedQuery(name="queryTest.tracksNoLongerThan",
                query="from Track as track where track.playTime <= :length")
    })
    public class Track  implements java.io.Serializable {

    @Id
    @Column(name="TRACK_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
     private int id;

    @Column(name="TITLE",nullable=false)
    @Index(name="TRACK_TITLE",columnNames={"TITLE"})
     private String title;

    @Column(nullable=false)
     private String filePath;

    @Temporal(TemporalType.TIME)
     private Date playTime;

    @Temporal(TemporalType.DATE)
     private Date added;

    @ManyToMany
    @JoinTable(name="TRACK_ARTISTS",
            joinColumns={@JoinColumn(name="ARTIST_ID")},
            inverseJoinColumns={@JoinColumn(name="TRACK_ID")})
     private Set<Artist> artists = new HashSet<Artist>(0);
     /**
      * Media on which track was obtained
     */
    @Enumerated(EnumType.STRING)
     private SourceMedia sourceMedia;

     @CollectionOfElements
     @JoinTable(name="TRACK_COMMENTS",
            joinColumns={@JoinColumn(name="TRACK_ID")})
    @Column(name="COMMENT")
     private Set<String> comments = new HashSet<String>(0);
     /**
      * How loud to play
     */
     @Embedded
     @AttributeOverrides({
         @AttributeOverride(name="left", column=@Column(name="VOL_LEFT")),
         @AttributeOverride(name="right", column=@Column(name="VOL_RIGHT"))
     })
     private StereoVolume volume;

    public Track() {
    }


    public Track(String title, String filePath) {
        this.title = title;
        this.filePath = filePath;
    }
    public Track(String title, String filePath, Date playTime, Date added, Set<Artist> artists, SourceMedia sourceMedia, Set<String> comments, StereoVolume volume) {
       this.title = title;
       this.filePath = filePath;
       this.playTime = playTime;
       this.added = added;
       this.artists = artists;
       this.sourceMedia = sourceMedia;
       this.comments = comments;
       this.volume = volume;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    public String getFilePath() {
        return this.filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public Date getPlayTime() {
        return this.playTime;
    }

    public void setPlayTime(Date playTime) {
        this.playTime = playTime;
    }
    public Date getAdded() {
        return this.added;
    }

    public void setAdded(Date added) {
        this.added = added;
    }
    public Set<Artist> getArtists() {
        return this.artists;
    }

    public void setArtists(Set<Artist> artists) {
        this.artists = artists;
    }
    /**       
     *      * Media on which track was obtained
     */
    public SourceMedia getSourceMedia() {
        return this.sourceMedia;
    }

    public void setSourceMedia(SourceMedia sourceMedia) {
        this.sourceMedia = sourceMedia;
    }
    public Set<String> getComments() {
        return this.comments;
    }

    public void setComments(Set<String> comments) {
        this.comments = comments;
    }
    /**       
     *      * How loud to play
     */
    public StereoVolume getVolume() {
        return this.volume;
    }

    public void setVolume(StereoVolume volume) {
        this.volume = volume;
    }

    /**
     * toString
     * @return String
     */
     public String toString() {
      StringBuffer buffer = new StringBuffer();

      buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
      buffer.append("title").append("='").append(getTitle()).append("' ");          
      buffer.append("sourceMedia").append("='").append(getSourceMedia()).append("' ");          
      buffer.append("volume").append("='").append(getVolume()).append("' ");            
      buffer.append("]");

      return buffer.toString();
     }



}

构建.xml

    <artifact:dependencies pathId="dependency.classpath">
        <dependency groupId="mysql" artifactId="mysql-connector-java"
            version="5.0.5" />

        <dependency groupId="org.hibernate" artifactId="hibernate"
            version="3.2.5.ga">
            <exclusion groupId="javax.transaction" artifactId="jta"></exclusion>
        </dependency>

        <dependency groupId="org.hibernate" artifactId="hibernate-annotations"
                version="3.3.0.ga">
        </dependency>

        <dependency groupId="org.hibernate" artifactId="hibernate-commons-annotations"
                version="3.3.0.ga">
        </dependency>

        <dependency groupId="org.hibernate" artifactId="hibernate-tools"
            version="3.2.0.beta9a" />


        <dependency groupId="org.apache.geronimo.specs" artifactId="geronimo-jta_1.1_spec"
            version="1.1" />
        <dependency groupId="log4j" artifactId="log4j" version="1.2.14" />

    </artifact:dependencies>

这是我在应用程序中使用的所有依赖项

我的错误跟踪是

    BUILD FAILED
    D:\Hibernate-SelfStudy\Workspace\HibernateTutorial\build.xml:19: org.hibernate.MappingException: Could not determine type for: java.util.Set, for colu
    mns: [org.hibernate.mapping.Column(comments)]
            at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
            at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:138)
            at org.hibernate.mapping.Column.getSqlType(Column.java:182)
            at org.hibernate.mapping.Table.sqlCreateString(Table.java:394)
            at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:848)
            at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:94)
            at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:61)
            at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
            at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:183)
            at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:601)
            at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
            at org.apache.tools.ant.Task.perform(Task.java:348)
            at org.apache.tools.ant.Target.execute(Target.java:392)
            at org.apache.tools.ant.Target.performTasks(Target.java:413)
            at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
            at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
            at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
            at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
            at org.apache.tools.ant.Main.runBuild(Main.java:811)
            at org.apache.tools.ant.Main.startAnt(Main.java:217)
            at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
            at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

    Total time: 3 seconds

有人可以帮我解决这个问题吗

在此先感谢基兰

4

2 回答 2

0

对不起这个问题。该错误是在其他类中引发的。错误地给出了评论集合的注释。

于 2013-01-07T00:29:38.447 回答
0

使用@ElementCollection 而不是@CollectionOfElements. 它被贬低了。

希望它有所帮助:)

于 2013-01-03T18:45:40.913 回答