我尝试仅为该字段提供@Column(name = "message")
,但它无效。
我有一个Bean类,包含10个字段,但我想使用HIBERNATE ANNOTATION只插入2个字段..? 我怎么能省略其余的字段?
我正在充分查询:
insert into message (circle, day, destNumber, encryptedBody, hour, interfaceType, location, message) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
相反,我只想要: 插入消息(消息,msgId)值(?,?)
答案 0 :(得分:1)
根据documentation,您需要注释不希望保留为@Transient
的属性:
@Entity
public class Message implements Serializable {
Long id;
int circle;
int day;
String message
//...
@Id
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
// Will not be persisted
@Transient
public int getCircle() { return circle; }
public void setCircle(int circle) { this.circle = circle; }
// Will not be persisted
@Transient
public int getDay() { return day; }
public void setDay(int day) { this.day = day; }
// Will be persisted
public string getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
//...
}
如果您需要自动生成Id
,则应使用@GeneratedValue
对其进行注释:
@Id
@GeneratedValue
@Column(name = "msgId")
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
@GeneratedValue
注释有许多参数可帮助您定义标识符的生成方式。例如,如果它将由数据库(标识列)生成,则应使用@GeneratedValue(strategy=GenerationType.IDENTITY)
。文档的This部分列出了所有生成策略。
请仔细阅读文档。您将在文档中找到几乎所有问题的答案。请参阅我提供的链接。
答案 1 :(得分:1)
使用动态插入或动态更新
dynamic-insert属性告诉Hibernate是否在SQL INSERT语句中包含null属性。
仅更新修改后的值使用动态更新选项
如果dynamic-update设置为true,则hibernate会在Hibernate的SQL更新语句中排除未修改的属性。
@Entity
@Table(name =“table”)
@ org.hibernate.annotations.Entity( dynamicUpdate = true)
public class Sample实现了java.io.Serializable {//}
答案 2 :(得分:0)
对于MsgId问题
1.确保您的MsgId字段设置为主键并自动增加
2.为MsgId添加这些注释
@Id
@GeneratedValue(策略= GenerationType.AUTO)
public Long getMsgId(){
return id;
}