gpt4 book ai didi

java - 如何通过外键获取数据?

转载 作者:行者123 更新时间:2023-11-30 22:22:12 25 4
gpt4 key购买 nike

我如何从外键(restaurant_id 和 channel_id)获取内容?我的意思不是 FK 值,而是 restaurant_id 的餐厅名称和 channel_id 的 channel 名称。

表格:

enter image description here

模型:

@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;
}

...

服务

@Service
public class BookingServiceImpl implements BookingService {

@Autowired
private BookingDao bookingDao;

@Override
@Transactional
public List<Booking> getBookings() {
return bookingDao.getBookings();
}

...

Controller

@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() 结果:

enter image description here

最佳答案

@Entity
@Table(name = "restaurant")
public class Restaurant {

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="restaurant")
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, mappedBy="channel")
private List<Booking> bookings;

}

@Entity
@Table(name = "booking")
public class Booking {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant_id")
private Restaurant restaurant;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "channel_id")
private Channel channel;

}

您可以将 HQL 与 session.createQuery() 一起使用.

这得到所有Booking急切地获取协会。您可以使用 Criteria也有 fetch。

from Booking b left join fetch b.restaurant left join fetch b.channel 

这会获取名称,您可以将其处理为 List<Object[]>或使用变压器到 DTO。

select booking.name, restaurant.name, channel.name
from Booking booking left join booking.restaurant restaurant left join booking.channel channel

关于java - 如何通过外键获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36357268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com