I HAVE MADE A PROJECT IN I HAVE MADE ONE - ONE RELATION MY CODE IS ALSO RIGHT FROM MY POINT
OF VIEW. AND WHEN I RUN IT IS GIVING ME THE MESSAGE AS BELOW:::::::
run: [EL Info]: 2012-02-27 09:42:36.466--ServerSession(26453377)--EclipseLink, version: Eclipse Persistence Services - 2.2.0.v20110202-r8913 [EL Info]: 2012-02-27 09:42:36.965--ServerSession(26453377)--file:/F:/SCHOOL/build/classes/_SCHOOLPU login successful Object: school.Profile[ profId=107 ] is not a known entity type. BUILD SUCCESSFUL (total time: 1 second)
THE .JAVA FILES ARE AS FOLLOWS:::::
Student.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package school; import java.io.Serializable; import javax.persistence.*; import javax.xml.bind.annotation.XmlRootElement; /** * * @author WAHEGURU */ @Entity @Table(name = "student", catalog = "student", schema = "") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"), @NamedQuery(name = "Student.findByStuId", query = "SELECT s FROM Student s WHERE s.stuId = :stuId"), @NamedQuery(name = "Student.findByTeacherId", query = "SELECT s FROM Student s WHERE s.teacherId = :teacherId"), @NamedQuery(name = "Student.findByStuName", query = "SELECT s FROM Student s WHERE s.stuName = :stuName"), @NamedQuery(name = "Student.findByClass1", query = "SELECT s FROM Student s WHERE s.class1 = :class1")}) public class Student implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @Column(name = "Stu_Id", nullable = false) private Integer stuId; @Basic(optional = false) @Column(name = "Teacher_Id", nullable = false) private long teacherId; @Basic(optional = false) @Column(name = "Stu_Name", nullable = false, length = 50) private String stuName; @Basic(optional = false) @Column(name = "Class", nullable = false, length = 10) private String class1; public Student() { } public Student(Integer stuId) { this.stuId = stuId; } public Student(Integer stuId, long teacherId, String stuName, String class1) { this.stuId = stuId; this.teacherId = teacherId; this.stuName = stuName; this.class1 = class1; } public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public long getTeacherId() { return teacherId; } public void setTeacherId(long teacherId) { this.teacherId = teacherId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getClass1() { return class1; } public void setClass1(String class1) { this.class1 = class1; } @Override public int hashCode() { int hash = 0; hash += (stuId != null ? stuId.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Student)) { return false; } Student other = (Student) object; if ((this.stuId == null && other.stuId != null) || (this.stuId != null && !this.stuId.equals(other.stuId))) { return false; } return true; } @Override public String toString() { return "school.Student[ stuId=" + stuId + " ]"; } }
PROFILE.JAVA
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package school; import java.io.Serializable; import javax.persistence.*; import javax.xml.bind.annotation.XmlRootElement; /** * * @author WAHEGURU */ @Entity @Table(name = "profile") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Profile.findAll", query = "SELECT p FROM Profile p"), @NamedQuery(name = "Profile.findByProfId", query = "SELECT p FROM Profile p WHERE p.profId = :profId"), @NamedQuery(name = "Profile.findByStuId", query = "SELECT p FROM Profile p WHERE p.stuId = :stuId"), @NamedQuery(name = "Profile.findByFName", query = "SELECT p FROM Profile p WHERE p.fName = :fName"), @NamedQuery(name = "Profile.findByPhone", query = "SELECT p FROM Profile p WHERE p.phone = :phone"), @NamedQuery(name = "Profile.findByAddress", query = "SELECT p FROM Profile p WHERE p.address = :address")}) public class Profile implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @Column(name = "Prof_Id") private Integer profId; @Basic(optional = false) @Column(name = "Stu_Id") private int stuId; @Basic(optional = false) @Column(name = "F_Name") private String fName; @Basic(optional = false) @Column(name = "Phone") private long phone; @Basic(optional = false) @Column(name = "Address") private String address; public Profile() { } public Profile(Integer profId) { this.profId = profId; } public Profile(Integer profId, int stuId, String fName, long phone, String address, Student student) { this.profId = profId; this.stuId = stuId; this.fName = fName; this.phone = phone; this.address = address; this.student=student; } public Integer getProfId() { return profId; } public void setProfId(Integer profId) { this.profId = profId; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getFName() { return fName; } public void setFName(String fName) { this.fName = fName; } public long getPhone() { return phone; } public void setPhone(long phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @OneToOne(cascade=CascadeType.ALL) private Student student; /** * @return the person */ public Student getStudent() { return student; } /** * @param person the person to set */ public void setStudent(Student student) { this.student = student; } @Override public int hashCode() { int hash = 0; hash += (profId != null ? profId.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Profile)) { return false; } Profile other = (Profile) object; if ((this.profId == null && other.profId != null) || (this.profId != null && !this.profId.equals(other.profId))) { return false; } return true; } @Override public String toString() { return "school.Profile[ profId=" + profId + " ]"; } }
SCHOOL.JAVA(MAIN)
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package school; /** * * @author WAHEGURU */ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; /** * * @author Administrator */ public class School { public static void main(String arg[]){ EntityManagerFactory emf=Persistence.createEntityManagerFactory("SCHOOLPU"); EntityManager em=emf.createEntityManager(); try{ EntityTransaction entr=em.getTransaction(); entr.begin(); Student stu=new Student(); stu.setStuId(6); stu.setTeacherId(123); stu.setStuName("KALA"); stu.setClass1("VDF"); /* em.persist(stu); entr.commit(); entr.begin(); */ Profile pf=new Profile(); pf.setProfId(107); pf.setStudent(stu); pf.setFName("Faltoo"); pf.setPhone(54565155); pf.setAddress("Khanna"); em.persist(pf); entr.commit(); System.out.println("Successfully added into database."); } catch(Exception e){ System.out.println(e.getMessage()); } finally{ em.close(); } } }
.XML FILE
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="SCHOOLPU" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>school.Profile</class> <class>school.Student</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/student"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="eclipselink.ddl-generation" value="create-tables"/> </properties> </persistence-unit> </persistence>
KINDLY GO THROUGH IT AND LETME KNOW THAT WHAT WERE THE DOUGHTS IN THIS CODE.