JPA,在不查询实体的情况下保存关系

时间:2017-08-16 07:28:59

标签: java spring hibernate spring-data-jpa

我有这个实体代表两个其他实体之间的关系。

我希望保存关系,而不必在保存之前获得角色和权限。

我喜欢像.save(roleId,privilegeId)这样的东西,所以在保存关系之前我不必点击数据库。

是否可能以及如何?

由于

 getDirectory.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            new DownloadingTask().execute();
            Log.i("FilePAthFirst",""+getFilesDir());
        }
    });
    btnGETDATA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String filename="Robotoo.ttf";

            getTypeface(filename);
         }
    });

    private Typeface getTypeface(String filename)
    {

        Typeface font;
        try
        {

            font = Typeface.createFromFile(getFilesDir() +"/"+filename);
            Log.i("FOnt found",""+font);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }


    return  font;
}

private class DownloadingTask  extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL(fonturl);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.connect();


            FileOutputStream fos = new FileOutputStream(getApplicationContext().getFilesDir()+ "Robotoo.ttf");
            Log.i("Download","complete");
            Log.i("FOS",""+fos.toString());

            InputStream is = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            outputFile = null;
            Log.e("Error", "Download Error Exception " + e.getMessage());
        }

        return null;
    }
}

这是此实体的Spring Data JPA存储库

@Entity
@Table(name = "security_role_privilege")
public class RolePrivilege implements Serializable {

  private static final long serialVersionUID = -4788066966268187121L;

  @EmbeddedId protected RolePrivilegeId id;

  @ManyToOne
  @JoinColumn(name = "role_id", insertable = false, updatable = false)
  protected Role role;

  @ManyToOne
  @JoinColumn(name = "privilege_id", insertable = false, updatable = false)
  protected Privilege privilege;

  protected RolePrivilege() {}


  public RolePrivilege(Role role, Privilege privilege) {
    super();
    this.role = role;
    this.privilege = privilege;
    this.id = new RolePrivilegeId(role.getId(), privilege.getId());
    this.privilege.getRolePrivileges().add(this);
    this.role.getRolePrivileges().add(this);
  }

  public RolePrivilegeId getId() {
    return this.id;
  }

  public Role getRole() {
    return this.role;
  }

  public Privilege getPrivilege() {
    return this.privilege;
  }
}

1 个答案:

答案 0 :(得分:0)

使用原始EntityManager,您可以使用em.getReference(Class<T> clazz, Object primaryKey)仅提取您要链接的实体的引用,而无需实际获取实体的数据。