-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: @OneToOne
PostPosted: Wed Mar 03, 2010 5:12 am 
Beginner
Beginner

Joined: Wed Mar 03, 2010 4:06 am
Posts: 21
//EDIT: Solved! Damn. I justed missed to set up the cascadeType....Now it works. Thank you anyway.

Hello,

I'm totaly frustrated. I have no idea what's wrong. I hope somebody can help me. I am trying to build a simple OneToOne relationship between the tables "address" and "customer". Unfortunately it doesn't work and I got the messages:

    10:02:56,129 DEBUG SQL:393 - insert into Customer (address, name) values (?, ?)
    Hibernate: insert into Customer (address, name) values (?, ?)
    10:02:56,172 WARN JDBCExceptionReporter:77 - SQL Error: 1048, SQLState: 23000
    10:02:56,173 ERROR JDBCExceptionReporter:78 - Column 'address' cannot be null
    Exception in thread "main" javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.package.model.Customer]

Here are the classes:

Code:
@Entity
public class Customer {
   @Id
   @GeneratedValue
   private Integer id;
   
   private String name;
   
   @OneToOne
   @JoinColumn(name="address")
   private Address address;
   
   public Customer(){}
   
   public Customer(String name, Address address){
      this.setName(name);
      this.setAdresse(address);
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public Address getAddress() {
      return address;
   }

   public void setAdresse(Address address) {      
      address.setCustomer(this);
      this.address = address;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

@Entity
@Table(name="address")
public class Address {

   @Id
   @GeneratedValue
   private Integer id;
   
   private String city;
   
   @OneToOne(mappedBy="address")
   private Customer customer;
   
   public Address(){}
   
   public Address(String city) {
      this.city = city;
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public Customer getCustomer() {
      return customer;
   }

   public void setCustomer(Customer customer) {
      this.customer = customer;
   }
}


And this is my TestClass:
Code:
public class CustomerTest {
   public static void main(String[] args) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersManager");
      EntityManager em = emf.createEntityManager();
      EntityTransaction tx = em.getTransaction();
      Customer cust = new Customer("Foo Bar", new Address("N.Y."));
      
      System.out.println(cust.getAddress().getCustomer().getName());
      
      tx.begin();
      em.persist(cust);
      tx.commit();
      em.close();
   }

}


I am using MySQL and I set up a foreign key column "address" in the table "customer" to "id" in table "address". May anyone tell me what to do or point me to the error reason? Thanks a lot.

Jonny


Top
 Profile  
 
 Post subject: Re: @OneToOne
PostPosted: Wed Mar 03, 2010 5:19 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
The problem is that you only persist the customer cust and forget to persist the Address entity.
Solution:
Either persist also the address explicitly or declare Cascade.PERSIST on the association,
so hibernate will perform the persist action for you implicitly


Top
 Profile  
 
 Post subject: Re: @OneToOne
PostPosted: Wed Mar 03, 2010 5:24 am 
Beginner
Beginner

Joined: Wed Mar 03, 2010 4:06 am
Posts: 21
Yeah I just found my mistake. Thanks a lot for your explaination and quick the answer. Thank you ;)

Jonny


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.