简体   繁体   中英

ejb3 toplink jpa 1.0 querying and sequencing

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

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. :) .

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 :) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM