hibernate w/derby simple crud


3

3rdshiftcoder

hi-

i am really new to hibernate.
i thought it would be cool to use in my hobby.
i want to make the code that deals with the db more readable in my projects.
i hope hibernate can help me in that regard.

i am trying to add a record?
i get an error that says attempting to modify an identity column.
i didnt specify the identity column in my constructor call.

i saw how to iterate through the table (read using HQL) but am looking
for an example of create,update, delete. i am missing the example
somewhere in my book (hibernate in action).

with delete i probably just set part of a collection to null....right?

if i can get help on this identity column issue, i may be able to figure the
examples out myself.
thank you,
jim

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Date d1 = new Date(2006-10-14);
Date d2 = new Date(2006-10-15);
Accounts accounts = new Accounts("boo",d1,d2);

session.save(accounts);
tx.commit();
session.close();

--------------------------------------------------------------------------
// pojo
package bcalcpkg;
// Generated Jan 6, 2007 1:38:27 AM by Hibernate Tools 3.2.0.beta8
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.GeneratedValue;
/**
* Accounts generated by hbm2java
*/
@Entity
@org.hibernate.annotations.Entity(
dynamicInsert = true, dynamicUpdate = true)
@Table(name = "ACCOUNTS", schema = "USER1", uniqueConstraints = {})
public class Accounts implements java.io.Serializable {
// Fields
@Id @GeneratedValue
@Column(name = "ACCOUNTID")
private int accountid;
@Column(name = "ACCOUNTNAME")
private String accountname;
@Column(name = "ACCOUNTOPEN")
private Date accountopen;
@Column(name = "ACCOUNTCLOSE")
private Date accountclose;


// Constructors
/** default constructor */
public Accounts() {
}
/** minimal constructor */
public Accounts(int accountid, String accountname, Date accountopen) {
this.accountid = accountid;
this.accountname = accountname;
this.accountopen = accountopen;
}
/** full constructor */
public Accounts( String accountname, Date accountopen,
Date accountclose) {
//this.accountid = accountid;
this.accountname = accountname;
this.accountopen = accountopen;
this.accountclose = accountclose;
}
//public Accounts(String text) {
//this.text = text;
//}

// Property accessors
@Id
@Column(name = "ACCOUNTID", unique = true, nullable = false, insertable =
true, updatable = true)
public int getAccountid() {
return this.accountid;
}
public void setAccountid(int accountid) {
this.accountid = accountid;
}
@Column(name = "ACCOUNTNAME", unique = false, nullable = true, insertable =
true, updatable = true, length = 50)
public String getAccountname() {
return this.accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
@Temporal(TemporalType.DATE)
@Column(name = "ACCOUNTOPEN", unique = false, nullable = true, insertable =
true, updatable = true, length = 10)
public Date getAccountopen() {
return this.accountopen;
}
public void setAccountopen(Date accountopen) {
this.accountopen = accountopen;
}
@Temporal(TemporalType.DATE)
@Column(name = "ACCOUNTCLOSE", unique = false, nullable = true, insertable
= true, updatable = true, length = 10)
public Date getAccountclose() {
return this.accountclose;
}
public void setAccountclose(Date accountclose) {
this.accountclose = accountclose;
}




}
 
Ad

Advertisements

3

3rdshiftcoder

// Property accessors
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ACCOUNTID", unique = true, nullable = false, insertable =
true, updatable = true)
public int getAccountid() {
return this.accountid;
}
public void setAccountid(int accountid) {
this.accountid = accountid;
}

Hi-

sorry if an answer on the newsgroup crossed.
this is what it looks like now in my pojo and it is working.
the pojo annotations were wrong.
now i am inserting values to the db.

thanks,
jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top