2

I'm new to Hibernate. I want to insert data into this hibernate entity:

@Entity
public class Invitation implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "invited_on")
    @Temporal(TemporalType.TIMESTAMP)
    private Date invitedOn;

    @Column(name = "invited_email")
    private String invitedEmail;

    @Column(name = "invitation_msg")
    private String invitationMessage;

    private Boolean status; //true:accepted || false:pending

    @ManyToOne
    @JoinColumn(name = "sent_by")
    private Person inviter;

// getters and setters
}

I tested this code:

public void insert()
{
    Query query = session.createQuery("insert into Invitation(invited_on, invited_email, invitation_msg, sent_by)");
    int result = query.executeUpdate();
}

What is the proper way to insert data? How I should create session Object?

3
  • docs.jboss.org/hibernate/orm/5.1/quickstart/html_single/… Commented Aug 4, 2016 at 14:03
  • hi Peter, I suggest you to read more documents from hibernate. You need more stuff, like gettting sessionFactory, define transactions, define database connection,.... get a basic example from internet. Commented Aug 4, 2016 at 14:04
  • I added mapping file. I only need to make insert statement Commented Aug 4, 2016 at 14:06

2 Answers 2

2

use this in the main class it works to insert data into table:

    Users user = new Users();
    user.setUid(1);
    user.setUfname("firstname");
    user.setUlname("Lastname");

    Configuration config = new Configuration()
       .configure()
       .addAnnotatedClass(Users.class);

    ServiceRegistry reg = new ServiceRegistryBuilder()
       .applySettings(config.getProperties())
       .buildServiceRegistry();

    SessionFactory sf = config.buildSessionFactory(reg);
    Session session = sf.openSession();

    Transaction tx = session.beginTransaction();
    session.save(user);

    tx.commit();
Sign up to request clarification or add additional context in comments.

Comments

1

You have Hibernate and it's tools USE THEM!!!


DISCLAIMER:

  • Example took from HERE
  • code written on the fly!!
  • I didnt use status attribute because is not mapped!

// don't need if you already got a session
Session session = HibernateUtil.getSessionFactory().openSession();

// start transaction
session.beginTransaction();

// create invitation Object
Invitation inv = new Invitation();
inv.setId(1L);
inv.setInvitedOn(new java.util.Date());
inv.setInvitedEmail("[email protected]");
inv.setInvitationMessage("come on!!!");
inv.setInviter(new Person("inviter"));  // hey! this is not valid for sure! :)

// Save the invitation to database
session.save(inv);

// Commit the transaction
session.getTransaction().commit();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.