Java @OneToMany/@ManyToOne如何使用表单保存数据
我是新手,希望在使用表单在数据库中保存一些数据时得到一些帮助。 我做了一些工作,这是两个@Entity类Java @OneToMany/@ManyToOne如何使用表单保存数据,java,spring-mvc,spring-data-jpa,spring-data,Java,Spring Mvc,Spring Data Jpa,Spring Data,我是新手,希望在使用表单在数据库中保存一些数据时得到一些帮助。 我做了一些工作,这是两个@Entity类 @Entity public class Artist { @Id @GeneratedValue private Integer id; private String name; @OneToMany(mappedBy = "artist") private List<Song> songs; // s
@Entity
public class Artist {
@Id
@GeneratedValue
private Integer id;
private String name;
@OneToMany(mappedBy = "artist")
private List<Song> songs;
// setters and getters goes here
}
和两个存储库
public interface ArtistRepository extends JpaRepository<Artist, Integer> {
}
并使用
@Service
public class ArtistService{
@Autowired
private ArtistRepository artistRepository;
@Autowired
private SongRepository songRepository;
@Override
public List<Artist> findAll() {
return artistRepository.findAll();
}
public Job findArtistById(Integer id) {
return artistRepository.findOne(id);
}
public Artist findArtistWithSong(Integer id) {
Artist artist= findArtistById(id);
artist.setSongs(songRepository.findByJob(artist));
return artist;
}
}
我在artists.jsp中将其视为
<c:forEach items="${artists}" var="artist">
<a href='<spring:url value="/artists/${artist.id}"/>'> ${artist.name} </a>
</c:forEach>
而artist-detail.jsp就这么简单
<h1>
${artist.name }
</h1>
<ul>
<c:forEach items="${artist.songs}" var="song">
<li>${song.title}</li>
</c:forEach>
</ul>
${artist.name}
- ${song.title}
现在我想用表单保存带有类别(已经添加或添加了新类别)的新艺术家,但我不知道,我发现了很多JavaScript内容,我不想用表单保存艺术家的简单方法。请提供任何帮助。听起来您想在服务器端使用spring/java等来完成这一切。。。如果为用户可以从中选择的类别添加字段,并为添加新类别添加另一个字段;您可以在服务器上解析并知道传入的内容。如果您允许用户选择多个类别,您就无法避免javascript允许用户为新类别添加另一个字段,除非您使用允许多个条目的textarea或jquery插件。无论哪种方式,您都必须在服务器上对此进行解析,以便在数据库中相应地创建条目 我将编写一个单独的服务来处理这个问题,并确保它是事务性的,这样,如果由于某种原因新类别无法插入,其他数据将不会插入。为了创建示例,我将重用您的代码:
public void dbInitializer() {
// INSERT CATEGORY FIRST
Category c = new Category();
c.setName("Heavy Metal");
categoryRepository.save(c);
Artist artist1 = new Artist();
artist1.setName("Rock Star");
artistRepository.save(artist1);
Song song1 = new Song();
song1.setTitle("Stand Up for Rock Star"); // Dummy/fake song name
song1.setArtist(artist1);
song1.setCategory(c);
songRepository.save(song1);
Song song2 = new Song();
song2.setTitle("Sit Down for Looser"); // Dummy/fake song name
song2.setArtist(artist1);
song2.setCategory(c);
songRepository.save(song1);
}
确保注释类
@Transactional
,您可能想查看并了解需要配置的内容。听起来您想在服务器端使用spring/java等来完成这一切。。。如果为用户可以从中选择的类别添加字段,并为添加新类别添加另一个字段;您可以在服务器上解析并知道传入的内容。如果您允许用户选择多个类别,您就无法避免javascript允许用户为新类别添加另一个字段,除非您使用允许多个条目的textarea或jquery插件。无论哪种方式,您都必须在服务器上对此进行解析,以便在数据库中相应地创建条目
我将编写一个单独的服务来处理这个问题,并确保它是事务性的,这样,如果由于某种原因新类别无法插入,其他数据将不会插入。为了创建示例,我将重用您的代码:
public void dbInitializer() {
// INSERT CATEGORY FIRST
Category c = new Category();
c.setName("Heavy Metal");
categoryRepository.save(c);
Artist artist1 = new Artist();
artist1.setName("Rock Star");
artistRepository.save(artist1);
Song song1 = new Song();
song1.setTitle("Stand Up for Rock Star"); // Dummy/fake song name
song1.setArtist(artist1);
song1.setCategory(c);
songRepository.save(song1);
Song song2 = new Song();
song2.setTitle("Sit Down for Looser"); // Dummy/fake song name
song2.setArtist(artist1);
song2.setCategory(c);
songRepository.save(song1);
}
确保对类@Transactional
进行注释,您可能希望查看并了解需要配置什么
<c:forEach items="${artists}" var="artist">
<a href='<spring:url value="/artists/${artist.id}"/>'> ${artist.name} </a>
</c:forEach>
@RequestMapping(value = "/artists/{id}", method = RequestMethod.GET)
public String showArtistDetail(Model model, @PathVariable Integer id) {
model.addAttribute("artist", artistService.findArtistbWithSongs(id));
return "artist-detail";
}
<h1>
${artist.name }
</h1>
<ul>
<c:forEach items="${artist.songs}" var="song">
<li>${song.title}</li>
</c:forEach>
</ul>
public void dbInitializer() {
// INSERT CATEGORY FIRST
Category c = new Category();
c.setName("Heavy Metal");
categoryRepository.save(c);
Artist artist1 = new Artist();
artist1.setName("Rock Star");
artistRepository.save(artist1);
Song song1 = new Song();
song1.setTitle("Stand Up for Rock Star"); // Dummy/fake song name
song1.setArtist(artist1);
song1.setCategory(c);
songRepository.save(song1);
Song song2 = new Song();
song2.setTitle("Sit Down for Looser"); // Dummy/fake song name
song2.setArtist(artist1);
song2.setCategory(c);
songRepository.save(song1);
}