Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用生成器模式的JPA实体图_Java_Hibernate_Jpa_Entity_Entitygraph - Fatal编程技术网

Java 使用生成器模式的JPA实体图

Java 使用生成器模式的JPA实体图,java,hibernate,jpa,entity,entitygraph,Java,Hibernate,Jpa,Entity,Entitygraph,我有一张这样的汽车桌: +----+------+--------------+-----------+----------+------+ | Id | Name | Description | Make | Model | Year | +----+------+--------------+-----------+----------+------+ | 1 | A | something1 | Ford | Explorer | 2010 | |

我有一张这样的汽车桌:

+----+------+--------------+-----------+----------+------+
| Id | Name | Description |   Make    |  Model   | Year |
+----+------+--------------+-----------+----------+------+
|  1 | A    | something1   | Ford      | Explorer | 2010 |
|  2 | B    | something2   | Nissan    | Ultima   | 2005 |
|  3 | C    | something3   | Chevrolet | Malibu   | 2012 |
+----+------+--------------+-----------+----------+------+
@Entity
@Table(schema = "dbo", name = "Car")
public class CarEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "Name")
    private String name;

    @Column(name = "Description")
    private String description;

    @Column(name = "Make")
    private String make;

    @Column(name = "Model")
    private String model;

    @Column(name = "Year")
    private int year;

    //Getters and Setters for all attributes
}
public interface CarRepository {
    CarRepository getCar();

    CarRepository withName();

    CarRepository withDescription();

    CarRepository withMake();

    CarRepository withModel();

    CarRepository withYear();

    List<Car> build(int carId);
}
@Repository
public class JpaCarRepository implements CarRepository {
    private static EntityGraph<CarEntity> eg;

    @Override
    public CarRepository getCar() {
        eg = this.em.createEntityGraph(CarEntity.class);
        eg.addAttributeNodes("id");
        return this;
    }

    @Override
    public CarRepository withName() {
        eg.addAttributeNodes("name");
        return this;
    }

    @Override
    public CarRepository withDescription() {
        eg.addAttributeNodes("description");
        return this;
    }

    //rest of the with methods

    @Override
    public List<Car> build(int carId) {
        List<CarEntity> entities = em.createQuery("SELECT c FROM dbo.Car c WHERE c.id = :carId")
                .setHint("javax.persistence.fetchgraph", eg)
                .getResultList();
        return //convert list of CarEntity to Car and return list;
    }
}
List<Car> cars = repo.getCar().withName().withDescription().build(1);
还有一个汽车实体,看起来像这样:

+----+------+--------------+-----------+----------+------+
| Id | Name | Description |   Make    |  Model   | Year |
+----+------+--------------+-----------+----------+------+
|  1 | A    | something1   | Ford      | Explorer | 2010 |
|  2 | B    | something2   | Nissan    | Ultima   | 2005 |
|  3 | C    | something3   | Chevrolet | Malibu   | 2012 |
+----+------+--------------+-----------+----------+------+
@Entity
@Table(schema = "dbo", name = "Car")
public class CarEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "Name")
    private String name;

    @Column(name = "Description")
    private String description;

    @Column(name = "Make")
    private String make;

    @Column(name = "Model")
    private String model;

    @Column(name = "Year")
    private int year;

    //Getters and Setters for all attributes
}
public interface CarRepository {
    CarRepository getCar();

    CarRepository withName();

    CarRepository withDescription();

    CarRepository withMake();

    CarRepository withModel();

    CarRepository withYear();

    List<Car> build(int carId);
}
@Repository
public class JpaCarRepository implements CarRepository {
    private static EntityGraph<CarEntity> eg;

    @Override
    public CarRepository getCar() {
        eg = this.em.createEntityGraph(CarEntity.class);
        eg.addAttributeNodes("id");
        return this;
    }

    @Override
    public CarRepository withName() {
        eg.addAttributeNodes("name");
        return this;
    }

    @Override
    public CarRepository withDescription() {
        eg.addAttributeNodes("description");
        return this;
    }

    //rest of the with methods

    @Override
    public List<Car> build(int carId) {
        List<CarEntity> entities = em.createQuery("SELECT c FROM dbo.Car c WHERE c.id = :carId")
                .setHint("javax.persistence.fetchgraph", eg)
                .getResultList();
        return //convert list of CarEntity to Car and return list;
    }
}
List<Car> cars = repo.getCar().withName().withDescription().build(1);
我有这样一个存储库层:

+----+------+--------------+-----------+----------+------+
| Id | Name | Description |   Make    |  Model   | Year |
+----+------+--------------+-----------+----------+------+
|  1 | A    | something1   | Ford      | Explorer | 2010 |
|  2 | B    | something2   | Nissan    | Ultima   | 2005 |
|  3 | C    | something3   | Chevrolet | Malibu   | 2012 |
+----+------+--------------+-----------+----------+------+
@Entity
@Table(schema = "dbo", name = "Car")
public class CarEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "Name")
    private String name;

    @Column(name = "Description")
    private String description;

    @Column(name = "Make")
    private String make;

    @Column(name = "Model")
    private String model;

    @Column(name = "Year")
    private int year;

    //Getters and Setters for all attributes
}
public interface CarRepository {
    CarRepository getCar();

    CarRepository withName();

    CarRepository withDescription();

    CarRepository withMake();

    CarRepository withModel();

    CarRepository withYear();

    List<Car> build(int carId);
}
@Repository
public class JpaCarRepository implements CarRepository {
    private static EntityGraph<CarEntity> eg;

    @Override
    public CarRepository getCar() {
        eg = this.em.createEntityGraph(CarEntity.class);
        eg.addAttributeNodes("id");
        return this;
    }

    @Override
    public CarRepository withName() {
        eg.addAttributeNodes("name");
        return this;
    }

    @Override
    public CarRepository withDescription() {
        eg.addAttributeNodes("description");
        return this;
    }

    //rest of the with methods

    @Override
    public List<Car> build(int carId) {
        List<CarEntity> entities = em.createQuery("SELECT c FROM dbo.Car c WHERE c.id = :carId")
                .setHint("javax.persistence.fetchgraph", eg)
                .getResultList();
        return //convert list of CarEntity to Car and return list;
    }
}
List<Car> cars = repo.getCar().withName().withDescription().build(1);
编辑2:因此我通过以下操作修复了上述问题:

@Override
public List<Car> build(int carId) {
    List<CarEntity> entities = em.createQuery("SELECT c FROM dbo.Car c WHERE c.id = :carId")
            .setHint("javax.persistence.fetchgraph", eg)
            .setParameter("id", carId)
            .getResultList();
    return //convert list of CarEntity to Car and return list;
}
@覆盖
公共列表生成(int carId){
List entities=em.createQuery(“从dbo.Car c中选择c,其中c.id=:carId”)
.setHint(“javax.persistence.fetchgraph”,例如)
.setParameter(“id”,carId)
.getResultList();
return//将CarEntity列表转换为Car和return列表;
}
但不幸的是,我认为构建器实际上没有工作,因为当我从查询中删除
withDescription()
时,我仍然得到一个填充了description字段的实体


如何让它只检索我指定的属性?

Your build()方法返回Car entity对象。因为它返回整个汽车数据对象,所以它拥有数据库中的所有数据字段。如果只想检索指定的属性,则需要将数据对象与已传递的Id一起传递给build()方法。完成后,您可以更改HQL查询以将属性容纳到其中。我希望我正确理解了你的问题。请告诉我您是否打算这样做。@RahulRaj No build()返回
Car
业务对象,正如您在
CarEntity
对象上方的代码中看到的那样,该对象是一个单独的对象。build方法获取
CarEntity
对象,然后在返回之前将其转换为业务对象。你能举一个例子说明你的意思吗,因为我没有理解你在数据对象中所说的“传递”的意思。好的,我理解这一部分。我可以知道您是否希望build()方法检索您指定的属性吗?@RahulRaj我只想使用我用
方法指定的属性检索汽车实体对象。因此,如果我说
repo.getCar().withName().build(1)
,那么它应该只返回表中id为1的汽车,并且只返回id和name属性。其余属性(description、model、make等)应该为null,因为它们没有被获取。需要澄清的是:setHint(“javax.persistence.fetchgraph”,例如)您传递的这个查询提示是否用于设置查询属性(您只想要由某些列组成的结果)?问这个是因为我以前没有用过entitygraph。让我知道。