java - JOIN with JPA: what queries should I write to get the same result I get in JDBC? -


i'm new jpa , trying convert method using in jdbc jpa exercise. supposed calculate masses of stars in database , update relative mass column. calculate mass need temperature of star , value of flux @ 350um band. problem have data saved in different tables of database, therefore have use join.

jdbc approach

public void updateallmasses() {     string selectquery = "select s.starid, s.temperature, f.value" +             " star s join flux f " +             "on s.starid = f.source " +             "where f.band = 350.00 , f.value != 0";     try {         resultset resultset = databaseutiljdbc.dbexecutequery(selectquery);         while (resultset.next()) {             string starid = resultset.getstring("starid");             double flux350 = resultset.getdouble("value");             double temp = resultset.getdouble("temperature");             if (temp != 0) {                 string updatequery = "update star " +                         "set mass = 0.053 * " + flux350 + " * (100) * (exp(41.14 / " + temp + " ) - 1) "                         + "where starid = '" + starid + "';";                 databaseutiljdbc.dbexecuteupdate(updatequery);             }         }     } catch (sqlexception e) {         e.printstacktrace();     } } 

jpa approach attempt

here need make somehow join between star object , flux object.

here's draft of star class.

@entity @table(name = "star") public class star implements serializable {     private static final long serialversionuid = 1l;     @id     @column(name = "starid", nullable = false)     private string starid;     @column(name = "temperature")     private bigdecimal temperature;     @column(name = "mass")     private bigdecimal mass;      // constructors, getters , setters  } 

here's entity class i'm using flux:

@entity @table(name = "flux") public class flux implements serializable {      private static final long serialversionuid = 1l;      @embeddedid private fluxid fluxid = new fluxid();     @column(name = "value")     private bigdecimal value;      // constructors, getters , setters  } 

with idclass:

@embeddable public class fluxid implements serializable {     private static final long serialversionuid = 1l;     @column(name = "source", nullable = false)     private string source;     @column(name = "band", nullable = false)     private bigdecimal band;      // constructors, getters , setters  } 

here's attempt jpa:

public void updateallmasses() {     string query = "???"; // query values need     list list = databaseutiljpa.dbexecutequery(query);     (object alist : list) {         star star = (star) alist; // here star object... while need it's flux value @ 350 band!         if (!star.gettemperature().equals(bigdecimal.valueof(0))) {             query = "update star star set star.mass = 0.053 * flux.value * 100 * (exp(41.14 / star.temperature) - 1)" +                     " star.starid = '" + star.getstarid() + "'";             databaseutiljpa.dbexecuteupdate(query);         }     } } 

what queries should write?

your model not mapped correctly. if want use joins jpa have declare them correctly. fluxid has this:

@embeddable public class fluxid implements serializable {    @manytoone   @joincolumn(name="source")   private star star;    @column(name = "band", nullable = false)   private bigdecimal band;    // constructors, getters , setters  } 

you mark flux class having separate idclass:

@entity @idclass(fluxid.class) public class flux {             @id   private source source;    @id   private bigdecimal band;   // constructors, getters , setters  } 

warning absolutely essential implement correct equals() method ensures object after persisting identical and identical if retrieve database.

make sure looks this:

  @override   public boolean equals(object obj) {      if (obj == this) return true;             if (obj == null || !(obj.getclass().equals(this.getclass())))         return false;      fluxid otherfluxid = (fluxid)obj;      if (this.getsource() == null             || otherfluxid.getsource() == null             || this.getband() == null             || otherfluxid.getband() == null)         return false;      return this.getsource().equals(otherfluxid.getsource()) &&             this.getband().equals(otherfluxid.getband()); }  @override public int hashcode() {      if (this.getsource() == null && this.getband() == null) return super.hashcode();      return (this.getsource() != null ? this.getsource().hashcode() : 0 ) ^            (this.getband() != null ? this.getband().hashcode() : 0 );  } 

please note implementation has flaw hashcode() before persisting different after persisting it. don't know how avoid have careful if store entity in collection before persisting it.

once have done query becomes:

select f.source.id, f.source.temperature, f.value flux f f.band = 350.00 , f.value != 0 

if want object can work object, too:

select f.source flux f f.band = 350.00 , f.value != 0 

(not 100% sure syntax of last one.)


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -