hibernate w/derby simple crud

Discussion in 'Java' started by 3rdshiftcoder, Jan 7, 2007.

  1. 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;
    }




    }
    3rdshiftcoder, Jan 7, 2007
    #1
    1. Advertising

  2. Re: hibernate w/derby simple crud: SOLVED

    // 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
    3rdshiftcoder, Jan 7, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,718
    Ilias Lazaridis
    Dec 27, 2004
  2. Replies:
    0
    Views:
    3,697
  3. Wills
    Replies:
    0
    Views:
    363
    Wills
    Jul 9, 2007
  4. Wills
    Replies:
    3
    Views:
    972
    Wills
    Jul 10, 2007
  5. Replies:
    8
    Views:
    1,925
Loading...

Share This Page