package org.aoron.model;public class Article implements java.io.Serializable { private Integer articleId;
private Integer catid;
private Integer userid;
private Integer priTagid;
private String title;
private String message;
private long releaseTime;
private Integer viewcount;
private Integer commentcount;
private String childTag;
private String password;
private byte sticky;
private Tag tag;
private Category cat ;
private User user ;
public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public Category getCat() {
return cat;
} public void setCat(Category cat) {
this.cat = cat;
} public Tag getTag() {
return tag;
} public void setTag(Tag tag) {
this.tag = tag;
} public Article() {
} public Article(Integer catid, Integer userid, Integer priTagid,
String title, String message, long releaseTime,
Integer viewcount, Integer commentcount, String childTag,
String password, byte sticky) {
this.catid = catid;
this.userid = userid;
this.priTagid = priTagid;
this.title = title;
this.message = message;
this.releaseTime = releaseTime;
this.viewcount = viewcount;
this.commentcount = commentcount;
this.childTag = childTag;
this.password = password;
this.sticky = sticky;
} public Integer getArticleId() {
return this.articleId;
} public void setArticleId(Integer articleId) {
this.articleId = articleId;
} public Integer getCatid() {
return this.catid;
} public void setCatid(Integer catid) {
this.catid = catid;
} public Integer getUserid() {
return this.userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public Integer getPriTagid() {
return this.priTagid;
} public void setPriTagid(Integer priTagid) {
this.priTagid = priTagid;
} public String getTitle() {
return this.title;
} public void setTitle(String title) {
this.title = title;
} public String getMessage() {
return this.message;
} public void setMessage(String message) {
this.message = message;
} public long getReleaseTime() {
return this.releaseTime;
} public void setReleaseTime(long releaseTime) {
this.releaseTime = releaseTime;
} public Integer getViewcount() {
return this.viewcount;
} public void setViewcount(Integer viewcount) {
this.viewcount = viewcount;
} public Integer getCommentcount() {
return this.commentcount;
} public void setCommentcount(Integer commentcount) {
this.commentcount = commentcount;
} public String getChildTag() {
return this.childTag;
} public void setChildTag(String childTag) {
this.childTag = childTag;
} public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} public byte getSticky() {
return this.sticky;
} public void setSticky(byte sticky) {
this.sticky = sticky;
}}
private Integer catid;
private Integer userid;
private Integer priTagid;
private String title;
private String message;
private long releaseTime;
private Integer viewcount;
private Integer commentcount;
private String childTag;
private String password;
private byte sticky;
private Tag tag;
private Category cat ;
private User user ;
public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public Category getCat() {
return cat;
} public void setCat(Category cat) {
this.cat = cat;
} public Tag getTag() {
return tag;
} public void setTag(Tag tag) {
this.tag = tag;
} public Article() {
} public Article(Integer catid, Integer userid, Integer priTagid,
String title, String message, long releaseTime,
Integer viewcount, Integer commentcount, String childTag,
String password, byte sticky) {
this.catid = catid;
this.userid = userid;
this.priTagid = priTagid;
this.title = title;
this.message = message;
this.releaseTime = releaseTime;
this.viewcount = viewcount;
this.commentcount = commentcount;
this.childTag = childTag;
this.password = password;
this.sticky = sticky;
} public Integer getArticleId() {
return this.articleId;
} public void setArticleId(Integer articleId) {
this.articleId = articleId;
} public Integer getCatid() {
return this.catid;
} public void setCatid(Integer catid) {
this.catid = catid;
} public Integer getUserid() {
return this.userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public Integer getPriTagid() {
return this.priTagid;
} public void setPriTagid(Integer priTagid) {
this.priTagid = priTagid;
} public String getTitle() {
return this.title;
} public void setTitle(String title) {
this.title = title;
} public String getMessage() {
return this.message;
} public void setMessage(String message) {
this.message = message;
} public long getReleaseTime() {
return this.releaseTime;
} public void setReleaseTime(long releaseTime) {
this.releaseTime = releaseTime;
} public Integer getViewcount() {
return this.viewcount;
} public void setViewcount(Integer viewcount) {
this.viewcount = viewcount;
} public Integer getCommentcount() {
return this.commentcount;
} public void setCommentcount(Integer commentcount) {
this.commentcount = commentcount;
} public String getChildTag() {
return this.childTag;
} public void setChildTag(String childTag) {
this.childTag = childTag;
} public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} public byte getSticky() {
return this.sticky;
} public void setSticky(byte sticky) {
this.sticky = sticky;
}}
private Integer fid;
private String cateName;
private String cateDesc;
private String path;
private String cateIcon;
private Integer sort;
private String url; // Constructors /** default constructor */
public Category() {
} /** full constructor */
public Category(Integer fid, String cateName, String cateDesc, String path,
String cateIcon, Integer sort, String url) {
this.fid = fid;
this.cateName = cateName;
this.cateDesc = cateDesc;
this.path = path;
this.cateIcon = cateIcon;
this.sort = sort;
this.url = url;
} // Property accessors public Integer getCatId() {
return this.catId;
} public void setCatId(Integer catId) {
this.catId = catId;
} public Integer getFid() {
return this.fid;
} public void setFid(Integer fid) {
this.fid = fid;
} public String getCateName() {
return this.cateName;
} public void setCateName(String cateName) {
this.cateName = cateName;
} public String getCateDesc() {
return this.cateDesc;
} public void setCateDesc(String cateDesc) {
this.cateDesc = cateDesc;
} public String getPath() {
return this.path;
} public void setPath(String path) {
this.path = path;
} public String getCateIcon() {
return this.cateIcon;
} public void setCateIcon(String cateIcon) {
this.cateIcon = cateIcon;
} public Integer getSort() {
return this.sort;
} public void setSort(Integer sort) {
this.sort = sort;
} public String getUrl() {
return this.url;
} public void setUrl(String url) {
this.url = url;
}}
<class name="org.aoron.model.Article" table="article">
<id name="articleId" type="java.lang.Integer">
<column name="articleId" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="500" not-null="true" />
</property>
<property name="msg" type="java.lang.String">
<column name="msg" length="65535" />
</property>
<many-to-one name="cat" calss="org.aoron.model.Category" column="catid" />
</class>
</hibernate-mapping> 然后按照上面配制把你的Category这个类Mapping配制出来,就可以了(注意懒加载问题)
可以查出对应的cat表的name
但是在写入时提示这样的错误:
02:59:59,875 ERROR JDBCExceptionReporter:78 - Column 'catid' cannot be null
org.hibernate.exception.ConstraintViolationException: could not insert: [org.aoron.model.Article]我想用 art = new Article();
art.getCat().getName();来查出cat表的name
但用many-to-one写后,article表不能写入数据。。
如果不用many-to-one就不能用上面的写法来查出cat的name数据。
你设置一下<many-to-one name="cat" calss="org.aoron.model.Category" column="catid" not-null="true"/>
试试
加了 not-null 属性了就变成这样了。。org.hibernate.PropertyValueException: not-null property references a null or transient value: org.aoron.model.Article.cat
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
not-null默认就是false,楼主检查一下数据库的那个字段是否允许为空的
not-null = false 是提示5L的错误如果不用many-to-one就不能实现5L的那个查询方法,晕啊~~
<hibernate-mapping>
<class name="org.aoron.model.Article" table="article">
<id name="articleId" type="java.lang.Integer">
<column name="articleId" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="500" not-null="true" />
</property>
<property name="msg" type="java.lang.String">
<column name="msg" length="65535" />
</property>
<many-to-one name="cat" column="catid"> </many-to-one>
</class>
</hibernate-mapping>
你配置了这个关系以后,你在pojo里面相应的也会配一个cat,你插入时就得用cat.catid来引用这个字段就OK了。
他有
private byte sticky;private Tag tag;
private Category cat ;
private User user ;
进行保存article的时候,要new一个Cat,设置好id,或者从数据库中读出一个,赋值给article,然后进行save
<class name="org.aoron.model.Article" table="article">
<id name="articleId" type="java.lang.Integer">
<column name="articleId" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="500" not-null="true" />
</property>
<property name="msg" type="java.lang.String">
<column name="msg" length="65535" />
</property>
<many-to-one name="cat" class="org.aoron.model.Category" column="catid" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping> 你最好吧调用代码贴出来
就是加上个lazy="false",class也要写成类cat类的全名。不加lazy="false",如果输出article.cat.catname,会报session is closed异常。