虽然我看到很多匹配的建议问题,没有一个建议似乎回答我的问题。我是Hibernate的新手,刚刚开始使用注释。稍微熟悉XML配置。我正在浏览"利用hibernate",并尝试一个注释示例。当试图运行这个,我被困在这个例外。我的代码在这里
hibernate.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>
Track.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();
}
}
build . 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-SelfStudyWorkspaceHibernateTutorialbuild.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
有人能帮我一下吗?
提前感谢Kiran
用@ElementCollection
代替@CollectionOfElements
。其depricated。
希望有帮助
不好意思。在其他类中抛出错误。注释集合的注释错误