0

I have 2 questions: suppose we have one entity named class and another called student. each class has onetomany students.

public class Clas implements Serializable {
  @Id
      @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;
@OneToMany(cascade=CascadeType.ALL)
Collection<Student> students;
  public clas(){
    super();
    }
 ..... getters and setters
}

q1: i get the exception there are no fields to be mapped, when adding any other column like String name, it works, but i don't need that field what can i do ?

q2: the ids is autogenerated, and i want to query all students in class c1, but i don't has the id of this class, how to do such query ?

iam working with mysql server glassfish v2.1 toplink jpa 1.0

Thanks

karthik
  • 17,453
  • 70
  • 78
  • 122
Tommy
  • 55
  • 1
  • 6

2 Answers2

0

The student class must have a property named 'classID' (or whatever) that refers to the

Clas's id property. That should be annotated like @ManyToOne.

If that's done already by IDE, then check id generation strategy. For example, if you are using mysql, the primary key is auto_increment, then set th id's strategy to

GenerationType.AUTO and recompile. Tell me if any other errors shows up. :) .

Acn
  • 1,010
  • 2
  • 11
  • 22
  • look i have the tables settled correctly and i persisted data into it and the association tables works fine, the problem is how would i query on these data like using the class id class 1 i need to get all students in this class 1 ??? getallstudents(Class c1) method > select s from student s, class c1 where c1.ID =:ID just like this ??? – Tommy Dec 26 '11 at 11:35
0

ok. I think I understood you question. You may use NamedQueries written in Query Languages dependent on your library (in your case toplink) like EJB QL or HBQL. You can create Session Beans for querying.

public class ClassSessionBean {
    @PersistenceContext(unitName="your PU name in persistence . xml")
    private Entitymanager em;

    publicClas selectByID(int id) throws NoResultException {
        Query q = em.createQuery("select class from Class class where class.id=?");
        q.setParameter(1, id);
        Clas clas = q.getResultList();
        return clas;
    }
} 

Note that the above code may contain syntax errors because I have not checked it anywhere.

Hope you find some help from this :) .

Acn
  • 1,010
  • 2
  • 11
  • 22