简体   繁体   中英

Java-ee programming; Confusion with JPA for 2 tables

I am using JPA to persist data. I am new to whole of Java-ee programming so please let me if I am doing it wrong way. I have 2 objects. Events and tickets. User will add new event and will select number of tickets for that event. Then I will be creating an entry in Event table. Also depending on number of tickets for that event, I will be creating so many entries in the tickets table. I am confused whether this logic of creating tickets when event gets created should be there in servlets or in the session bean. I have separate session bean for event and ticket. I am not sure if I should have 2 session beans or just 1 bean. Any pointers on this will be really helpful.

Code

@Entity
public class Event {    
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long eventId;
private String name;
@OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
private Set<Ticket> tickets;

    //getter and setters here
}    

@Entity
public class Ticket {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ticketId;
@ManyToOne
@JoinColumn(name = "event_id", nullable=false)
private Event event;

//getters and setters
 }

Your main logic should be in your EJBs, not in your controllers/servlets. Your EJBs should provide methods for your main use cases. You should only put presentation logic in servlets, formatting data for display and handling user actions.

See also: Session Facade pattern

As for the number of beans, that is your choice, you can start with one and refactor if it gets too messy.

Also, I'm not sure you should be creating tickets when you create an event. An event can have a field that says how many tickets are available, and you create a ticket when a user buys it, or something like that.

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