Hibernate插入包含未提及的列

Hibernate插入包含未提及的列,hibernate,Hibernate,我有一个映射到表的简单类。没什么特别的,但是我有四个表列,它们没有在类中引用。这是一种典型的模式,它在应用程序中的其他任何地方都可以使用。此外,它一直在工作,直到桌子上有一个小变化。现在,Hibernate包含了类中未引用的四个表列中的一个,这导致了空约束冲突(该列具有默认值;接下来,它将由触发器提供) 这是表定义(对于DDL很抱歉,SQL开发人员不会从网格中“复制”)。除modifiedby和modifieddate外,所有列都具有“NOTNULL”约束 CREATE TABLE "XYZOW

我有一个映射到表的简单类。没什么特别的,但是我有四个表列,它们没有在类中引用。这是一种典型的模式,它在应用程序中的其他任何地方都可以使用。此外,它一直在工作,直到桌子上有一个小变化。现在,Hibernate包含了类中未引用的四个表列中的一个,这导致了空约束冲突(该列具有默认值;接下来,它将由触发器提供)

这是表定义(对于DDL很抱歉,SQL开发人员不会从网格中“复制”)。除modifiedby和modifieddate外,所有列都具有“NOTNULL”约束

CREATE TABLE "XYZOWNER"."COMMENTM2" (
"COMMENTID" NUMBER(18,0), 
"CREATEDBY" VARCHAR2(255 CHAR) DEFAULT '{none provided}', 
"CREATEDDATE" DATE DEFAULT sysdate, 
"MODIFIEDBY" VARCHAR2(255 CHAR), 
"MODIFIEDDATE" DATE, 
"CREATEDONDATE" DATE, 
"CREATEDONTIME" DATE, 
"CREATEDBYADMIN" NUMBER(1,0) DEFAULT 0, 
"USERNAME" VARCHAR2(100 CHAR), 
"MKTPARTICIPANTID" NUMBER(18,0), 
"REFAPP" VARCHAR2(20 CHAR), 
"REFID" NUMBER(18,0), 
"TEXT" VARCHAR2(2000 CHAR)
) ;
这是课堂

package org.midwestiso.marketapps.mect2.entities;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * This class implements the Comment entity.
 * <p>
 * Note: the database table is named CommentM2, as "comment" is a reserved word.
 */
@Entity
@Table(name = "mect2owner.commentm2")
public class Comment {
/** DB ID */
private Long commentId;
/** Created on date (midnight GMT) */
private Date createdOnDate;
/** Created on time (1970-01-01 GMT) */
private Date createdOnTime;
/** Created by admin? */
private Boolean createdByAdmin;
/** User name */
private String userName;
/** Market Participant */
private MarketParticipant mktParticipant;
/** Reference application */
private String refApp;
/** Reference application ID */
private Long refId;
/** Text */
private String text;

/**
 * Return the commentId
 * 
 * @return the commentId
 */
@Id
@SequenceGenerator(name = "sequence", sequenceName = "mect2owner.seq_commentm2")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
public Long getCommentId() {
    return commentId;
}

/**
 * Return the createdOnDate <br />
 * Time set to midnight GMT
 * 
 * @return the createdOnDate
 */
public Date getCreatedOnDate() {
    return createdOnDate;
}

/**
 * Return the createdOnTime <br />
 * Date set to 1970-01-01
 * 
 * @return the createdOnTime
 */
public Date getCreatedOnTime() {
    return createdOnTime;
}

/**
 * Return the createdOn date/time as a String
 * 
 * @return the createdOn date/time as a String
 */
@Transient
public String getCreatedOn() {
    final String createdOn;
    final Calendar calendar;

    // Set the calendar to market time (EST)
    calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT-5"));
    // Set the calendar to the created on date + time
    calendar.setTimeInMillis(createdOnDate.getTime() + createdOnTime.getTime());

    createdOn =
            "" + calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-"
                    + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                    + calendar.get(Calendar.MINUTE);

    return createdOn;
}

/**
 * Return the createdByAdmin
 * 
 * @return the createdByAdmin
 */
public Boolean getCreatedByAdmin() {
    return createdByAdmin;
}

/**
 * Return the userName
 * 
 * @return the userName
 */
@Column(name = "createdBy")
public String getUserName() {
    return userName;
}

/**
 * Return the mktParticipant
 * 
 * @return the mktParticipant
 */
@ManyToOne
@JoinColumn(name = "mktParticipantId")
public MarketParticipant getMktParticipant() {
    return mktParticipant;
}

/**
 * Return the refApp
 * 
 * @return the refApp
 */
public String getRefApp() {
    return refApp;
}

/**
 * Return the refId
 * 
 * @return the refId
 */
public Long getRefId() {
    return refId;
}

/**
 * Return the text
 * 
 * @return the text
 */
public String getText() {
    return text;
}

/**
 * Set the commentId (limited scope)
 * 
 * @param commentIdParm
 *           the commentId to set
 */
void setCommentId(final Long commentIdParm) {
    commentId = commentIdParm;
}

/**
 * Set the createdOnDate (limited scope)
 * 
 * @param createdOnDateParm
 *           the createdOnDate to set
 */
void setCreatedOnDate(final Date createdOnDateParm) {
    createdOnDate = createdOnDateParm;
}

/**
 * Set the createdOnTime (limited scope)
 * 
 * @param createdOnTimeParm
 *           the createdOnTime to set
 */
void setCreatedOnTime(final Date createdOnTimeParm) {
    createdOnTime = createdOnTimeParm;
}

/**
 * Set the created on date and time to the current date
 */
public void setCreatedOn() {
    final Date now = new Date();
    final Calendar calendar;

    // Set up a Calendar with GMT timezone and the current date
    calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    calendar.setTime(now);

    // Adjust the Calendar to midnight
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    createdOnDate = calendar.getTime();
    createdOnTime = new Date(now.getTime() - createdOnDate.getTime());
}

/**
 * Set the createdByAdmin
 * 
 * @param createdByAdminParm
 *           the createdByAdmin to set
 */
public void setCreatedByAdmin(final Boolean createdByAdminParm) {
    createdByAdmin = createdByAdminParm;
}

/**
 * Set the userName
 * 
 * @param userNameParm
 *           the userName to set
 */
public void setUserName(final String userNameParm) {
    userName = userNameParm;
}

/**
 * Set the mktParticipant
 * 
 * @param mktParticipantParm
 *           the mktParticipant to set
 */
public void setMktParticipant(final MarketParticipant mktParticipantParm) {
    mktParticipant = mktParticipantParm;
}

/**
 * Set the refApp
 * 
 * @param refAppParm
 *           the refApp to set
 */
public void setRefApp(final String refAppParm) {
    refApp = refAppParm;
}

/**
 * Set the refId
 * 
 * @param refIdParm
 *           the refId to set
 */
public void setRefId(final Long refIdParm) {
    refId = refIdParm;
}

/**
 * Set the text
 * 
 * @param textParm
 *           the text to set
 */
public void setText(final String textParm) {
    text = textParm;
}
}

现在我不是Hibernate方面的专家,但我相信您这次遇到问题的原因是您使用
@Column
对其进行了注释


您可以尝试添加
@Generated(GenerationTime.insert)
,或者将
@列更改为包含
insertable=false,updateable=false
(假设您也不希望它包含在更新中)

当您使用
@Entity
注释类时,然后,每个成员都映射到表,但使用
@Transient
注释的成员除外。请参阅

这意味着您必须用
@Transient
在insert语句中注释您不希望的列


我之所以喜欢映射文件而不是注释,有一些原因;-)

几个小时内我无法自我回答,但问题是getUserName()有一个旧的注释。对不起……这是真的,但这不是问题所在。所有字段都应该映射到一列。事实上,您的注释是正确的!这是旧版本表/类的遗留。{尴尬图标}
insert into xyzowner.commentm2 
(createdByAdmin, createdOnDate, createdOnTime, mktParticipantId, refApp, refId, text, 
createdBy, commentId) 
values (?, ?, ?, ?, ?, ?, ?, ?, ?)