Nasıl içerik formu yabancı anahtarları (restaurant_id ve channel_id) alabilirim? FK değeri değil, restaurant_id için restoran adı ve channel_id için kanal adı.nasıl Yabancı anahtardan veri almak için?
Tabeles:
Modeller:
@Entity
@Table(name = "restaurant")
public class Restaurant {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "restaurant_id")
private List<Booking> bookings;
...
@Entity
@Table(name = "channel")
public class Channel {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "channel_id")
private List<Booking> bookings;
...
@Entity
@Table(name = "booking")
public class Booking {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "restaurant_id", unique = true)
private Long restaurant_id;
@Column(name = "channel_id", unique = true)
private Long channel_id;
...
@Override
public String toString() {
return "Booking [id=" + id + ", name=" + name + ", count=" + count + ", duration=" + duration + ", start="
+ start + ", phone=" + phone + ", comment=" + comment + ", updated=" + updated + ", created=" + created
+ ", active=" + active + ", restaurant_id=" + restaurant_id + ", channel_id=" + channel_id + "]";
}
...
DAO
@Repository
public class BookingDaoImpl implements BookingDao {
private static final Logger logger = LoggerFactory.getLogger(BookingDaoImpl.class);
@Resource(name = "localSessionFactoryBean")
private SessionFactory sessionFactory;
@Override
public List<Booking> getBookings() {
Session session = this.sessionFactory.getCurrentSession();
List<Booking> bookingList = session.createCriteria(Booking.class).list();
for (Booking booking : bookingList) {
logger.info("Booking List::" + booking);
}
return bookingList;
}
...
Servis
@Service
public class BookingServiceImpl implements BookingService {
@Autowired
private BookingDao bookingDao;
@Override
@Transactional
public List<Booking> getBookings() {
return bookingDao.getBookings();
}
...
Kontrolör
@Controller
public class MainController {
@Autowired
private BookingService bookingService;
@RequestMapping(value = "bookings", method = RequestMethod.GET)
public String bookings(Model model) {
List<Booking> bookingList = bookingService.getBookings();
model.addAttribute("bookings", bookingList);
return "bookings";
}
...
JSP
<c:forEach items="${bookings}" var="booking">
<tr>
<td>${booking}</td>
</tr>
</c:forEach>
toString() sonucu:
Bilgileri tam olarak nereden alıyorsunuz? JSP? Denetleyici veya bir SQL sorgusunda mı? – dubes
JSP. Şu anda ben restaurant_id = 1, channel_id = 1 olsun, ama restoran adı veya adresi ve kanal adını almak istiyorum. Id ile çok yapamam. – BrainDead