I am trying out some EJB3 in Action examples using Glassfish4 (EclipseLink) + JavaDB. So I have the below relationship
#Entity
#Table(name = "ITEMS")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private Long itemId;
...
private List<Bid> bids= new ArrayList<>();
#Id
#Column(name="ITEM_ID")
public Long getItemId() {
return itemId;
}
public void setItemId(Long itemId) {
this.itemId = itemId;
}
#OneToMany(mappedBy="item",fetch=FetchType.EAGER)
#JoinColumn(name="BID_ITEM_ID",referencedColumnName="ITEM_ID")
public List<Bid> getBids() {
return bids;
}
public void setBids(List<Bid> bids) {
this.bids = bids;
}
}
#Entity
#Table(name="BIDS")
public class Bid implements Serializable{
private static final long serialVersionUID = 1L;
...
private Item item;
...
#Id
#Column(name="BID_ID")
public Long getBidId() {
return bidId;
}
public void setBidId(Long bidId) {
this.bidId = bidId;
}
#ManyToOne
#JoinColumn(name="BID_ITEM_ID",referencedColumnName="ITEM_ID")
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
...
}
Now when fetching an Item like
#Override
public List<Bid> getBidsForItem(long itemId) {
Item item = em.find(Item.class, itemId); // em -> Entity manager
return item.getBids();
}
the item.getBids() returns an empty list (size = 0, not null). What changes should be done to get Bids for the given Item?
EDIT:
After enabling query logging as suggested in the comments
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
I notice that Queries are listed for insert statements but NO query is listed corresponding to em.find(Item.class, itemId).
EDIT 2 (ANSWER):
The problem was in my addBids() stateless bean function to which I was passing an Item object. This meant the Item Object is never in persistent context. The right way is to
pass the itemId
find the Item entity with entity manager find() method. This ensures that the Item object is in persistence context.
add bid object to item and item to bid
call entity manager persist() on Bid.
Corrected addBids() method:
public Bid addBids(Date bidDate, Double bidPrice, long itemId, String bidder) {
Item item = em.find(Item.class, itemId);
Bid bid = new Bid(bidDate, bidPrice, item, bidder);
item.getBids().add(bid);
em.persist(bid);
return bid;
}
Thanks to #Chris for pointing out.
Try instantiating a ArrayList<Bid> and assigning it to the List<Bid> declaration.
#OneToMany(mappedBy="item")
protected List<Bid> bids = new ArrayList<Bid>();
Try this :-
public class Item{
private List<Bid> bids= new ArrayList<>();
public void setBids(List<Bid> bids) {
for (Bid bid : bids) {
bid.setItem(this);
}
this.bids = bids;
}
}
Here you are freeing the client to make the relationship. Do the otherway round in Bid class also. But make sure you won't end up with infinite to and fro method calls.
And its a good approach to provide an add and remove method.
like :- public class Item{
private List<Bid> bids= new ArrayList<>();
public void addBid(Bid bid) {
bid.setItem(this);
this.bids.add(bids);
}
}
Related
I have following classes:
public interface Label {
String getValue();
}
#Node
#Data
public class SimpleLabel implements Label {
#Id
#GeneratedValue
private Long id;
private String value;
#Relationship(value = "JE")
private Set<SimpleLabel> labels = new HashSet<>();
public void addLabel(SimpleLabel label){
labels.add(label);
}
}
#Node
#Data
public class HyperLabel implements Label {
#Id
#GeneratedValue
private Long id;
#Relationship(value = "JE")
private Set<Label> labels = new HashSet<>();
public void addLabel(Label label){
labels.add(label);
}
#Override
public String getValue() {
return labels.stream().map(Label::getValue).collect(Collectors.joining("; "));
}
}
and this is the query I'm using:
#Query("MATCH (h:HyperLabel)-[r:JE]-(s:SimpleLabel) WHERE ID(h)=$id RETURN h,COLLECT(r),COLLECT(s)")
Optional<HyperLabel> getById(Long id);
The problem, I'm having is that labels collection that is returned by query is empty. But if I make a slight change in the code of HyperLabel class (change Set<Label> to Set<SimpleLabel>), then it works.
Is there any way to make it work with Label interface, since children of HyperLabel can be both HyperLabel and SimpleLabel instances?
Well, I'm using Hibernate for the first time and, unexpectedly, it works. Except for one thing: an insert with a pk already inserted overwrite the record instaed of preventing it.
That's my simple code:
#Controller
public class SimpleController {
#Autowired
UserRepository userRepository;
#GetMapping("/mainPage")
public String viewMainPage(){
return "mainPage";
}
#GetMapping("/nuovo-utente")
public String viewInserisciUtente(Model model){
model.addAttribute("nuovoUtente", new Utente());
return "nuovo-utente";
}
#PostMapping("/nuovo-utente")
public String memorizzaUtente(#ModelAttribute Utente utente){
userRepository.save(utente);
return "output";
}
}
#Entity
public class Utente {
#Id
private int id;
private String citta=null;
private String genere=null;
private String data_nascita=null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCitta() {
return citta;
}
public void setCitta(String citta) {
this.citta = citta;
}
public String getGenere() {
return genere;
}
public void setGenere(String genere) {
this.genere = genere;
}
public String getData_nascita() {
return data_nascita;
}
public void setData_nascita(String data_nascita) {
this.data_nascita = data_nascita;
}
}
Any help will be appreciated.
EDIT: I've added the entity class to help you understanding my problem. Hoping that this will help.
Thanks you all
If you look at CrudRepository documentation, then we don't have update method, but we only have save method, which is used to add or update existing records.
In your case, you might have updated an entity (except its Id field) and tried saving the entity. So, CrudRepository will update the existing value for given Id, since it is already present.
Try adding ID generation strategy to id field.
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
I'm currently working on a project where I'm trying to get a list of enities from table which does not have a primary key (dk_systemtherapie_merkmale). This table is 1:n related to another table (dk_systemtherapie). See the screenshot for the table structure.
When getting an entry for dk_systemtherapie, the program fetches the Collection "dkSystemtherapieMerkmalesById". However, the first table entry is fetched as often as the number of actual entries in the table is. It never fetches the other entries from dk_systemtherapie_merkmale. I assume it has something to do with the fact that hibernate can't differ between the entries, but I don't know how to fix it.
Table schema
I've created two corresponding entity classes, dk_systemtherapie:
#Entity
#Table(name = "dk_systemtherapie", schema = "***", catalog = "")
public class DkSystemtherapieEntity {
private int id;
private Collection<DkSystemtherapieMerkmaleEntity> dkSystemtherapieMerkmalesById;
#Id
#Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#OneToMany(mappedBy = "dkSystemtherapieByEintragId")
public Collection<DkSystemtherapieMerkmaleEntity> getDkSystemtherapieMerkmalesById() {
return dkSystemtherapieMerkmalesById;
}
public void setDkSystemtherapieMerkmalesById(Collection<DkSystemtherapieMerkmaleEntity> dkSystemtherapieMerkmalesById) {
this.dkSystemtherapieMerkmalesById = dkSystemtherapieMerkmalesById;
}
}
Here the second one, which is accessing the table without a primary key, dk_systhemtherapie_merkmale:
#Entity #IdClass(DkSystemtherapieMerkmaleEntity.class)
#Table(name = "dk_systemtherapie_merkmale", schema = "***", catalog = "")
public class DkSystemtherapieMerkmaleEntity implements Serializable {
#Id private Integer eintragId;
#Id private String feldname;
#Id private String feldwert;
private DkSystemtherapieEntity dkSystemtherapieByEintragId;
#Basic
#Column(name = "eintrag_id")
public Integer getEintragId() {
return eintragId;
}
public void setEintragId(Integer eintragId) {
this.eintragId = eintragId;
}
#Basic
#Column(name = "feldname")
public String getFeldname() {
return feldname;
}
public void setFeldname(String feldname) {
this.feldname = feldname;
}
#Basic
#Column(name = "feldwert")
public String getFeldwert() {
return feldwert;
}
public void setFeldwert(String feldwert) {
this.feldwert = feldwert;
}
#Id
#ManyToOne
#JoinColumn(name = "eintrag_id", referencedColumnName = "id")
public DkSystemtherapieEntity getDkSystemtherapieByEintragId() {
return dkSystemtherapieByEintragId;
}
public void setDkSystemtherapieByEintragId(DkSystemtherapieEntity dkSystemtherapieByEintragId) {
this.dkSystemtherapieByEintragId = dkSystemtherapieByEintragId;
}
}
I assume the problem is releated to the fact that Hibernate is using the following annotation as the one and only id for fetching data from database.
#Id
#ManyToOne
#JoinColumn(name = "eintrag_id", referencedColumnName = "id")
public DkSystemtherapieEntity getDkSystemtherapieByEintragId() {
return dkSystemtherapieByEintragId;
}
This leads to the problem that when getting more than one entry with the same id (as the id is not unique), you will get the number of entries you would like to but hibernate is always fetching the first entry for this id. So in fact you are getting dublicate entries.
So how to fix this?
According to this question: Hibernate and no PK, there are two workarounds which are actually only working when you don't have NULL entries in your table (otherwise the returning object will be NULL as well) and no 1:n relationship. For my understanding, hibernate is not supporting entities on tables without primary key (documentation). To make sure getting the correct results, I would suggest using NativeQuery.
Remove the Annotations and private DkSystemtherapieEntity dkSystemtherapieByEintragId; (incl. beans) from DkSystemtherapieMerkmaleEntity.java und add a constructor.
public class DkSystemtherapieMerkmaleEntity {
private Integer eintragId;
private String feldname;
private String feldwert;
public DkSystemtherapieMerkmaleEntity(Integer eintragId, String feldname, String feldwert) {
this.eintragId = eintragId;
this.feldname = feldname;
this.feldwert = feldwert;
}
public Integer getEintragId() {
return eintragId;
}
public void setEintragId(Integer eintragId) {
this.eintragId = eintragId;
}
public String getFeldname() {
return feldname;
}
public void setFeldname(String feldname) {
this.feldname = feldname;
}
public String getFeldwert() {
return feldwert;
}
public void setFeldwert(String feldwert) {
this.feldwert = feldwert;
}
}
Remove private Collection<DkSystemtherapieMerkmaleEntity> dkSystemtherapieMerkmalesById; (incl. beans) from DkSystemtherapieEntity.java.
Always when you need to get entries for a particular eintrag_id, use the following method instead of the Collection in DkSystemtherapieEntity.java.
public List<DkSystemtherapieMerkmaleEntity> getDkSystemtherapieMerkmaleEntities(int id) {
Transaction tx = session.beginTransaction();
String sql = "SELECT * FROM dk_systemtherapie_merkmale WHERE eintrag_id =:id";
List<Object[]> resultList;
resultList = session.createNativeQuery(sql)
.addScalar("eintrag_id", IntegerType.INSTANCE)
.addScalar("feldname", StringType.INSTANCE)
.addScalar("feldwert", StringType.INSTANCE)
.setParameter("id", id).getResultList();
tx.commit();
List<DkSystemtherapieMerkmaleEntity> merkmale = new ArrayList<>();
for (Object[] o : resultList) {
merkmale.add(new DkSystemtherapieMerkmaleEntity((Integer) o[0], (String) o[1], (String) o[2]));
}
return merkmale;
}
Call getDkSystemtherapieMerkmaleEntities(dkSystemtherapieEntityObject.getid()) instead of getDkSystemtherapieMerkmalesById().
I have a spring boot (1.5.4.RELEASE) project using Java 8. I have an entity and it's related domain class like this:
#Entity
#Table(name = "Foo", schema = "dbo")
public class FooEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Id")
private int id;
#Column(name="Name")
private String name;
#Column(name="Type")
private String type;
#Column(name="Color")
private String color;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Car")
private Car car;
//getter and setter
}
public class Foo {
private int id;
private String name;
private String type;
private String color;
private Car car;
//Constructors and getters
}
I want to create a repository that fetches this Foo object from the DB but only fetching the complex fields if the user asks for them to prevent unnecessary join statements. The repo looks like this:
import static com.test.entities.QFooEntity.fooEntity;
import static com.test.entities.QCarEntity.carEntity;
#Repository
public class FooRepository {
private final JPAQuery<FooEntity> query = createQuery().from(fooEntity);
public FooRepository getFooByName(String name) {
query.where(fooEntity.name.eq(name));
return this;
}
public FooRepository withCar() {
query.leftJoin(fooEntity.car, carEntity).fetchJoin();
return this;
}
public Foo fetch() {
FooEntity entity = query.fetchOne();
return FooMapper.mapEntityToDomain().apply(entity);
}
}
So a barebones call for a Foo object will return the Entity with values for all the fields except for the car field. If the user wants car information then they have to explicitly call withCar.
Here is the mapper:
public class FooMapper {
public static Function<FooEntity, Foo> mapEntityToDomain() {
return entity -> {
return new Foo(e.getId(), e.getName(), e.getType(), e.getColor(), e.getCar());
};
}
}
The problem is when you do e.getCar() if the value is not there (i.e. there's a proxy present) JPA will go out and fetch it for you. I don't want this to be the case. It will just grab the values and map them to the domain equivalent if it's not there then null.
One solution that I've heard (and tried) is calling em.detach(entity); however, this doesn't work as I intended because it throws an exception when you try to access getCar and I've also heard this is not best practice.
So my question is what is the best way to create a repo using a builder pattern on a JPA entity and not have it call the DB when trying to map.
You could create a utility method that will return null if the given object is a proxy and is not initialized:
public static <T> T nullIfNotInitialized(T entity) {
return Hibernate.isInitialized(entity) ? entity : null;
}
Then you can call the method wherever you need it:
return new Foo(e.getId(), e.getName(), e.getType(), e.getColor(), nullIfNotInitialized(e.getCar()));
Just map it to a new object and leave out the Car relation, this is the standard approach. You can use MapStruct and just ignore the car field during mapping: http://mapstruct.org/documentation/stable/reference/html/#inverse-mappings
Just don't map the car... Map a field holding the ID and use another method to get the actual Car. I would use a distinctive method name, to differentiate it from the other getters.
class FooEntity {
#Column
private int carId;
public int getCarId() {
return carId;
}
public void setCarId(int id) {
this.carId = id;
}
public Car fetchCar(CarRepository repo) {
return repo.findById(carId);
}
}
You can write query on top of JPA
#Query("select u from Car c")
import org.springframework.data.repository.CrudRepository;
import com.example.model.FluentEntity;
public interface DatabaseEntityRepository extends CrudRepository<FooEntity , int > {
}
As you said
I don't want this to be the case. It will just grab the values and map them to the domain equivalent, if it's not there then null.
Then you just set it to null, because the field car will always not be there.
Otherwise, if you mean not there is that the car not exists in db, for sure a subquery(call the proxy) should be made.
If you want to grab the car when call Foo.getCar().
class Car {
}
class FooEntity {
private Car car;//when call getCar() it will call the proxy.
public Car getCar() {
return car;
}
}
class Foo {
private java.util.function.Supplier<Car> carSupplier;
public void setCar(java.util.function.Supplier<Car> carSupplier) {
this.carSupplier = carSupplier;
}
public Car getCar() {
return carSupplier.get();
}
}
class FooMapper {
public static Function<FooEntity, Foo> mapEntityToDomain() {
return (FooEntity e) -> {
Foo foo = new Foo();
foo.setCar(e::getCar);
return foo;
};
}
}
Make sure you have the db session ,when you call Foo.getCar()
You could try adding state to your repository and influence the mapper. Something like this:
import static com.test.entities.QFooEntity.fooEntity;
import static com.test.entities.QCarEntity.carEntity;
#Repository
public class FooRepository {
private final JPAQuery<FooEntity> query = createQuery().from(fooEntity);
private boolean withCar = false;
public FooRepository getFooByName(String name) {
query.where(fooEntity.name.eq(name));
return this;
}
public FooRepository withCar() {
query.leftJoin(fooEntity.car, carEntity).fetchJoin();
withCar = true;
return this;
}
public Foo fetch() {
FooEntity entity = query.fetchOne();
return FooMapper.mapEntityToDomain(withCar).apply(entity);
}
}
In your mapper, you then include a switch to enable or disable car lookups:
public class FooMapper {
public static Function<FooEntity, Foo> mapEntityToDomain(boolean withCar) {
return e -> {
return new Foo(e.getId(), e.getName(), e.getType(), e.getColor(), withCar ? e.getCar() : null);
};
}
}
If you then use new FooRepository().getFooByName("example").fetch() without the withCar() call, e.getCar() should not be evaluated inside FooMapper
You may want to use the PersistentUnitUtil class to query if an attribute of entity object is already loaded or not. Based on that you may skip the call to corresponding getter as shown below. JpaContext you need to supply to user entity bean mapper.
public class FooMapper {
public Function<FooEntity, Foo> mapEntityToDomain(JpaContext context) {
PersistenceUnitUtil putil = obtainPersistentUtilFor(context, FooEntity.class);
return e -> {
return new Foo(
e.getId(),
e.getName(),
e.getType(),
e.getColor(),
putil.isLoaded(e, "car") ? e.getCar() : null);
};
}
private PersistenceUnitUtil obtainPersistentUtilFor(JpaContext context, Class<?> entity) {
return context.getEntityManagerByManagedType(entity)
.getEntityManagerFactory()
.getPersistenceUnitUtil();
}
}
#Entity
public class Bid {
#Id
#GeneratedValue
#Column(name = "bid_id")
private Long bidId;
#Column(name = "bid_amt")
private double bidAmount;
#Basic(fetch = FetchType.LAZY, optional = false)
private String person;
#ManyToOne(targetEntity = Item.class, fetch = FetchType.LAZY)
#JoinColumn(name = "bid_item", referencedColumnName = "item_id", nullable = false)
private Item item;
public Long getBidId() {
return bidId;
}
public double getBidAmount() {
return bidAmount;
}
public String getPerson() {
return person;
}
public Item getItem() {
return item;
}
public void setBidAmount(final double bidAmount) {
this.bidAmount = bidAmount;
}
public void setPerson(final String person) {
this.person = person;
}
public void setItem(final Item item) {
this.item = item;
}
public Bid() {
}
#Override
public String toString() {
return "Bid [bidId=" + bidId + ", bidAmount=" + bidAmount + ", person="
+ person + /* ", item=" + item + */"]";
}
}
Test Case:
#Test
public void testBidRead() {
final Session currentSession = sessionFactory.getCurrentSession();
final List<Bid> bids = currentSession.createQuery("from Bid").list();
for (final Bid bid : bids) {
System.out.println(bid);
}
}
And the SQL the Hibernate generated is:
/*
from
Bid */ select
bid0_.bid_id as bid1_1_,
bid0_.bid_amt as bid2_1_,
bid0_.bid_item as bid4_1_,
bid0_.person as person1_
from
Bid bid0_
Question: Although I marked person attribute as lazy && optional, why is it part of SQL query? Does this mean that Hibernate is not fetching lazily? Same is the case with Item.
How do I fetch attributes lazily?
To make the person attribute (as opposed to association) truly lazy, you must bytecode instrument your classes at build time. The reference documentation has some information on how to do it.
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#performance-fetching-lazyproperties
Person is a single (string) property
Fetching it while other fetching (for the non-lazy fetches like bidId and bidAmount) is done on the same object is common sense
As the query has to be executed anyway and transporting a varchar (or whatever) along with the bidId and bidAmount is not much overhead
And it loads in the item id (not the item itself) so that the item itself can be loaded when you call getItem() without another query to get the id (caching the id in between the construction and getItem() call)
It seems that hibernate is loading just foreign key values, not the whole Person or Item. What's wrong with it?