zoukankan      html  css  js  c++  java
  • jpa一对多

    package com.itheima;

    import com.itheima.dao.RoleDao;
    import com.itheima.dao.UserDao;
    import com.itheima.domain.Role;
    import com.itheima.domain.User;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.annotation.Rollback;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.transaction.annotation.Transactional;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:beans.xml")
    public class AppTest {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Test
    @Transactional
    @Rollback(false)
    public void testSave() {//一对多
    //创建数据
    User user = new User("小明", "123", 28);
    //创建角色
    Role role = new Role("父亲", "赚钱养娃");


    //数据产生关系
    user.getRoles().add(role);
    role.getUsers().add(user);

    //保存了
    userDao.save(user);
    roleDao.save(role);


    }


    @Test
    @Transactional
    @Rollback(false)
    public void testSave1() {
    //创建数据
    User user = new User("小明", "123", 28);
    //创建角色
    Role role = new Role("父亲", "赚钱养娃");


    //数据产生关系
    user.getRoles().add(role);
    role.getUsers().add(user);


    roleDao.save(role);
    }

    @Test
    @Transactional
    @Rollback(false)
    public void testRemove() {
    Role role = roleDao.findById(1).get();

    roleDao.delete(role);

    }


    }

    积少成多, 积沙成塔.
  • 相关阅读:
    ASP.net:Literal控件用法
    css如何自动换行对于div,p等块级元素(转)
    java ftp操作类
    java文件操作类
    geoserver图层显示
    java csv读取
    geoserver 源码编译(转)
    ArcGIS Engine 空间运算
    ArcMap操作技巧
    geoserver开发资料收集
  • 原文地址:https://www.cnblogs.com/lei0913/p/10969719.html
Copyright © 2011-2022 走看看