public static void main(String[] args) { Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all cars @SuppressWarnings("unchecked") Car car = (Car) session.createQuery("from Car c where id =1").uniqueResult(); car.setPrice(car.getPrice() + 50); tx.commit(); System.out.println( "brand= " + car.getBrand() + ", year= " + car.getYear() + ", price= " + car.getPrice()); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } // Close the SessionFactory (not mandatory) sessionFactory.close(); System.exit(0); }
public static void main(String[] args) { // Create an instance of Car with year as 2015 Car c = new Car(2015); // Create a Tire for that car of 9.0 inch radius Car.Tire t = c.new Tire(9.0); System.out.println("Car's year:" + c.getYear()); System.out.println("Car's tire radius:" + t.getRadius()); }
// Writes cars into a text file before closing @Override protected void finalize() throws FileNotFoundException, IOException { File file = new File(this.fileName); FileWriter fileWriter = new FileWriter(file); BufferedWriter bWriter = new BufferedWriter(fileWriter); try { for (Car car : cars) { if (car != null) { bWriter.write(car.getMake() + System.getProperty("line.separator")); bWriter.write(car.getYear() + System.getProperty("line.separator")); bWriter.write(car.getPrice() + System.getProperty("line.separator")); bWriter.write(car.getVin() + System.getProperty("line.separator")); } } } catch (Exception e) { } finally { bWriter.close(); fileWriter.close(); } }
@Test public void testProjection() throws IOException { Path path = writeCarsToParquetFile(1, CompressionCodecName.UNCOMPRESSED, false); Configuration conf = new Configuration(); Schema schema = Car.getClassSchema(); List<Schema.Field> fields = schema.getFields(); // Schema.Parser parser = new Schema.Parser(); List<Schema.Field> projectedFields = new ArrayList<Schema.Field>(); for (Schema.Field field : fields) { String name = field.name(); if ("optionalExtra".equals(name) || "serviceHistory".equals(name)) { continue; } // Schema schemaClone = parser.parse(field.schema().toString(false)); Schema.Field fieldClone = new Schema.Field(name, field.schema(), field.doc(), field.defaultValue()); projectedFields.add(fieldClone); } Schema projectedSchema = Schema.createRecord( schema.getName(), schema.getDoc(), schema.getNamespace(), schema.isError()); projectedSchema.setFields(projectedFields); AvroReadSupport.setRequestedProjection(conf, projectedSchema); ParquetReader<Car> reader = new AvroParquetReader<Car>(conf, path); for (Car car = reader.read(); car != null; car = reader.read()) { assertEquals(car.getDoors() != null, true); assertEquals(car.getEngine() != null, true); assertEquals(car.getMake() != null, true); assertEquals(car.getModel() != null, true); assertEquals(car.getYear() != null, true); assertEquals(car.getVin() != null, true); assertNull(car.getOptionalExtra()); assertNull(car.getServiceHistory()); } }
public void postVehicle(Car car) throws JSONException { /* dialog = new ProgressDialog(this); dialog.setMessage("Loading Messages... Please wait..."); dialog.show(); */ final Car carToAdd = car; JSONObject params = new JSONObject(); // params.put("alt", "json"); params.put("username", Global.localUser.getUsername()); params.put("plateNumber", car.getLicensePlateNumber()); params.put("plateState", car.getLicensePlateState()); params.put("make", car.getMake()); params.put("model", car.getModel()); params.put("year", car.getYear()); params.put("color", car.getColor()); StringEntity entity = new StringEntity(params.toString(), ContentType.APPLICATION_JSON); Global.localUser.addCar(carToAdd); ParkingRestClient.post( this, "addVehicle?", entity, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // dialog.dismiss(); Log.d("POST VEHICLE", "Success with object response"); Log.d("POST VEHICLE", response.toString()); try { JSONObject key = response.getJSONObject("key"); int id = Integer.parseInt(key.getString("id")); carToAdd.setId(id); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { Global.localUser.addCar(carToAdd); Log.d("POST VEHICLE", "Success with array response"); Log.d("POST VEHICLE", response.toString()); } @Override public void onFailure( int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) { // dialog.dismiss(); Global.localUser.getCars().remove(carToAdd); System.out.println(errorResponse); Toast toast = Toast.makeText(getBaseContext(), "Error connecting to Server", Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 25, 400); toast.show(); } }); }