Reputation: 427
I tried to give @Column(name = "message")
only for the field, but it didn't work.
I have a Bean class, that contains 10 fields but i want to insert only 2 fields using HIBERNATE ANNOTATION..? How can i omit rest of the fields??
I am getting full Query:
insert into message (circle, day, destNumber, encryptedBody, hour, interfaceType, location, message) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Instead i want only: insert into message (message,msgId) values(?,?)
Upvotes: 2
Views: 5402
Reputation: 18453
According to the documentation, you need to annotate the properties that you don't want to be persisted as @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; }
//...
}
If you need the Id
to be auto-generated, you should annotate it with @GeneratedValue
:
@Id
@GeneratedValue
@Column(name = "msgId")
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
The @GeneratedValue
annotation has many parameters that help you define how the identifier should be generated. For example, if it will be generated by the database (identity column) you should use @GeneratedValue(strategy=GenerationType.IDENTITY)
. This section of the documentation lists all of the generation strategies.
Please read the documentation carefully. You'll find the answer to almost all of your questions in the documentation. Refer to the links that I've provided.
Upvotes: 1
Reputation: 1942
For the MsgId issue
1.Make sure that your MsgId field is set to Primary key and auto increment
2.add these annotations for MsgId
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getMsgId() {
return id;
}
Upvotes: 0
Reputation: 1942
Use Dynamic insert or dynamic-update
The dynamic-insert attribute tells Hibernate whether to include null properties in the SQL INSERT statement.
To update only modified values Use dynamic-update option
If dynamic-update set to true, then hibernate excludes unmodified properties in the Hibernate’s SQL update statement.
@Entity
@Table(name = "table")
@org.hibernate.annotations.Entity( dynamicUpdate = true)
public class Sample implements java.io.Serializable { // }
Upvotes: 2