我正在开发一个 Restful 的服务项目,我试图通过服务持久化一个实体,但我不知道如何编写引用另一个对象的参数...这是我的类的一些代码.
域类
@Component
@Entity
public class Atleta extends Persona implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private Skill skill
private Result result
}
<小时/>
@Entity
public class Skill implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private string description
public Skill() {
}
public Skill(Atleta atleta, String description) {
super();
this.atleta = atleta;
this.description = description;
}
//..Getters and Setters
Dao 接口(interface)
public interface DaoAtletaI extends JpaRepository<Atleta, Integer> {
}
服务
@
Controller
@RequestMapping(value="/equipos")
public class ServicieAtleta {
@Autowired
private DaoAtletaI iAtleta;
@RequestMapping(value="/insert")
public @ResponseBody Atleta insertrAtleta (@RequestParam(value="skill", required= true ) ??? ????,
@RequestParam(value="result", required= true ) ??? ???
)
{
Atleta atl = iAtleta.saveAndFlush(new Atleta(???? , ????);
return atl;
}
}
如何编写引用对象的参数?
有人可以帮我解决这个问题吗?
最佳答案
您只能运行 update and delete modifying queries :
@Modifying
@Transactional
@Query("delete from User u where u.active = false")
void deleteInactiveUsers();
这是因为 Hibernate 仅支持这些 DML 风格的批处理操作。
对于插入,您必须使用 EntityManager 持久和合并以及自定义存储库方法。
引用实体时需要使用实体 ID。 REST服务必须以skillId作为参数来创建Atelta实体。
Skill skill = iAtleta.findOne(skillId);
Atleta atl = iAtleta.saveAndFlush(new Atleta(skill , result);
关于java - 使用 Spring Jpa 为服务插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048704/