【问题标题】:@OneToMany/@ManyToOne how to save data using form@OneToMany/@ManyToOne 如何使用表单保存数据
【发布时间】:2015-12-03 04:42:26
【问题描述】:

我是新手,想在使用表单将一些数据保存在数据库中时获得一些帮助。 我已经完成了一些工作,即两个 @Entity 类

@Entity
public class Artist {


    @Id
    @GeneratedValue
    private Integer id;

    private String name;     

    @OneToMany(mappedBy = "artist")
    private List<Song> songs;

    // setters and getters goes here

}

@Entity
public class Song{

    @Id
    @GeneratedValue
    private Integer id;

    private String title;

    @ManyToOne
    @JoinColumn(name = "artist_id")
    private Artist artist;

    // setters an getters goes here

}

和两个存储库

public interface ArtistRepository extends JpaRepository<Artist, Integer> {

}

public interface SongRepository extends JpaRepository<Song, Integer> {
   List<Song> findByArtist(Artist artist);
}

我可以使用以下类保存数据

@Transactional
@Service
public class DatabaseServiceInitializer {

    @Autowired
    private ArtistRepository  artistRepository;

    @Autowired
    private SongRepository songRepository;

    @PostConstruct
    public void dbInitializer() {

        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);       
        songRepository.save(song1);

        Song song2 = new Song();            
        song2.setTitle("Sit Down for Looser"); // Dummy/fake song name
        song2.setArtist(artist1);       
        songRepository.save(song1);         

    }
}

并使用

取回这些数据
@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;
    }    
}

使用这个控制器

@Controller
public class ArtistController {

    @Autowired
    private ArtistService artistService;        

    @RequestMapping(value = "/artists", method = RequestMethod.GET)
    public String showArtists(Model model) {
        model.addAttribute("artists", artistServicefindAll());
        return "artists";
    }
}

我在 Artists.jsp 中将其视为

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

artist-detail.jsp 和

一样简单
<h1>
    ${artist.name }
</h1> 

    <ul>
    <c:forEach items="${artist.songs}" var="song">
        <li>${song.title}</li>
    </c:forEach>
    </ul>

现在我想使用表单保存带有类别的新艺术家(已添加或添加新类别),但我不知道,我找到了This So Question answer,但有很多我不想要的 JavaScript 东西我想要使用表单保存艺术家的简单方法。请帮忙。

【问题讨论】:

    标签: java spring-mvc spring-data-jpa spring-data


    【解决方案1】:

    听起来你想在服务器端完成这一切,使用 spring/java 等......如果你为用户可以选择的类别添加一个字段,并为添加新类别添加另一个字段;您可以在服务器上解析并知道正在传递的内容。如果您允许用户选择多个类别,则除非您使用 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,您可能需要查看Using @Transactional 并了解您需要配置的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      相关资源
      最近更新 更多