This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JPA foreign key  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is a Basic JPA program of one to one mapping Here when i am running the program Primary key of Customer class i.e cid is 1 But ID of Address i.e aid and F.K sid is 2 I want aid and sid to be 1 instead of 2

This is main class

public class Hibernate {

public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session se=sf.openSession();
Transaction tx=se.beginTransaction();
Customer c=new Customer();
c.setCname("srikant");
Address a=new Address();
a.setState("karnatka");
c.setAdd(a);
se.save(c);
tx.commit();
}}




@Entity
@Table(name="Cust")
public class Customer {

@Id
  @GeneratedValue
  @Column(name="cid")
  private int cid;

  private String cname;

  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name="sid") // Foreign Key
  private Address add;
  public int getCid() {
  return cid;
  }
  public void setCid(int cid) {
  this.cid = cid;
  }
  public String getCname() {
   return cname;
  }
    public void setCname(String cname) {
    this.cname = cname;
    }
    public Address getAdd() {
    return add;
    }
     public void setAdd(Address add) {
    this.add = add;
     }
    @Override
    public String toString() {
     return "Customer [cid=" + cid + ", cname=" + cname + ", add=" + add +
      "]";
}




  My Address Class

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

   private int aid;
   private String state;
   @Id
    @GeneratedValue
    public int getAid() {
    return aid;
     }
    public void setAid(int aid) {
    this.aid = aid;
   }
    public String getState() {
  return state;
     }
   public void setState(String state) {
    this.state = state;
   }

   @Override
   public String toString() {
    return "Address [aid=" + aid + ", state=" + state +  "]";
      }


         }
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!