@Test public void testBindBean() { assertThat( dao.insert(ImmutableTrain.builder() .name("Zephyr") .carriages(8) .observationCar(true) .build())) .isEqualTo(1); assertThat(dao.getTrains().get(0)) .extracting("name", "carriages", "observationCar") .containsExactly("Zephyr", 8, true); }
/** * Builds a new {@link ImmutableTrain ImmutableTrain}. * @return An immutable instance of Train * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableTrain build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableTrain(name, carriages, observationCar); }
@Test public void testBindBean() { assertThat( dao.insert(ImmutableTrain.builder() .name("Zephyr") .carriages(8) .observationCar(true) .build())) .isEqualTo(1); assertThat(dao.getTrains().get(0)) .extracting("name", "carriages", "observationCar") .containsExactly("Zephyr", 8, true); }
@Test public void simpleTest() { jdbi.getConfig(JdbiImmutables.class).registerImmutable(Train.class); try (Handle handle = jdbi.open()) { handle.execute("create table train (name varchar, carriages int, observation_car boolean)"); assertThat( handle.createUpdate("insert into train(name, carriages, observation_car) values (:name, :carriages, :observationCar)") .bindPojo(ImmutableTrain.builder().name("Zephyr").carriages(8).observationCar(true).build()) .execute()) .isEqualTo(1); assertThat( handle.createQuery("select * from train") .mapTo(Train.class) .findOnly()) .extracting("name", "carriages", "observationCar") .containsExactly("Zephyr", 8, true); } } // end::example[]
/** * Creates an immutable copy of a {@link ImmutablesTest.Train} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable Train instance */ public static ImmutableTrain copyOf(ImmutablesTest.Train instance) { if (instance instanceof ImmutableTrain) { return (ImmutableTrain) instance; } return ImmutableTrain.builder() .from(instance) .build(); }
/** * Fill a builder with attribute values from the provided {@code Train} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(ImmutablesTest.Train instance) { Objects.requireNonNull(instance, "instance"); name(instance.name()); carriages(instance.carriages()); observationCar(instance.observationCar()); return this; }
/** * Creates a builder for {@link ImmutableTrain ImmutableTrain}. * @return A new ImmutableTrain builder */ public static ImmutableTrain.Builder builder() { return new ImmutableTrain.Builder(); }