Skip to content

Instantly share code, notes, and snippets.

@donovanmuller
Created June 30, 2014 15:54
Show Gist options
  • Save donovanmuller/d9b06edc2bfb00e0fe64 to your computer and use it in GitHub Desktop.
Save donovanmuller/d9b06edc2bfb00e0fe64 to your computer and use it in GitHub Desktop.
SO - 24489963 - Non mappedBy
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class OrderServiceTest {
private static final Logger log = LoggerFactory.getLogger(OrderServiceTest.class);
@Configuration
@ComponentScan(basePackages = "so")
static class OrderServiceConfiguration {
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManager(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setShowSql(true);
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan("so");
return entityManagerFactory;
}
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase dataSource = builder.setType(EmbeddedDatabaseType.H2).build();
return dataSource;
}
}
@PersistenceContext
private EntityManager em;
@Inject
private OrderService orderService;
@Test
@Transactional
public void persist_order_successfully() {
log.debug(">>> Persisting original SO question Order");
WaiterEntity waiter = new WaiterEntity();
waiter.setName("Test Waiter");
em.persist(waiter);
OrderEntity order = new OrderEntity();
order.setSumOfMoney(100);
order.setWaiter(waiter);
orderService.create(order);
log.debug("<<<Persisted original SO question Order");
em.clear(); // <-- force show SQL query used and not session reference
log.debug(">>> Adding OrderEntity maintaining both relationships");
WaiterEntity waiterEntity = em.find(WaiterEntity.class, 1L);
OrderEntity orderEntity = new OrderEntity();
orderEntity.setSumOfMoney(200);
orderEntity.setWaiter(waiter);
waiterEntity.getOrders().add(orderEntity);
em.persist(waiterEntity);
em.flush();
em.clear();
log.debug(">>> Adding OrderEntity without maintaining inverse side");
waiterEntity = em.find(WaiterEntity.class, 1L);
orderEntity = new OrderEntity();
orderEntity.setSumOfMoney(300);
orderEntity.setWaiter(waiterEntity);
em.persist(orderEntity);
em.flush();
em.clear();
assertThat(em.find(WaiterEntity.class, 1L).getOrders().size(), IsEqual.equalTo(3));
}
}
[main] DEBUG so.services.OrderServiceTest - >>> Persisting original SO question Order
Hibernate:
insert
into
WAITERENTITY
(id, name)
values
(null, ?)
Hibernate:
insert
into
ORDERENTITY
(orderNumber, finalized, sumOfMoney, waiter_id)
values
(null, ?, ?, ?)
[main] DEBUG so.services.OrderServiceTest - <<<Persisted original SO question Order
[main] DEBUG so.services.OrderServiceTest - >>> Adding OrderEntity maintaining both relationships
Hibernate:
select
waiterenti0_.id as id1_1_0_,
waiterenti0_.name as name2_1_0_,
orders1_.waiter_id as waiter_i4_1_1_,
orders1_.orderNumber as orderNum1_0_1_,
orders1_.orderNumber as orderNum1_0_2_,
orders1_.finalized as finalize2_0_2_,
orders1_.sumOfMoney as sumOfMon3_0_2_,
orders1_.waiter_id as waiter_i4_0_2_
from
WAITERENTITY waiterenti0_
left outer join
ORDERENTITY orders1_
on waiterenti0_.id=orders1_.waiter_id
where
waiterenti0_.id=?
Hibernate:
insert
into
ORDERENTITY
(orderNumber, finalized, sumOfMoney, waiter_id)
values
(null, ?, ?, ?)
Hibernate:
update
ORDERENTITY
set
waiter_id=?
where
orderNumber=?
[main] DEBUG so.services.OrderServiceTest - >>> Adding OrderEntity without maintaining inverse side
Hibernate:
select
waiterenti0_.id as id1_1_0_,
waiterenti0_.name as name2_1_0_,
orders1_.waiter_id as waiter_i4_1_1_,
orders1_.orderNumber as orderNum1_0_1_,
orders1_.orderNumber as orderNum1_0_2_,
orders1_.finalized as finalize2_0_2_,
orders1_.sumOfMoney as sumOfMon3_0_2_,
orders1_.waiter_id as waiter_i4_0_2_
from
WAITERENTITY waiterenti0_
left outer join
ORDERENTITY orders1_
on waiterenti0_.id=orders1_.waiter_id
where
waiterenti0_.id=?
Hibernate:
insert
into
ORDERENTITY
(orderNumber, finalized, sumOfMoney, waiter_id)
values
(null, ?, ?, ?)
Hibernate:
select
waiterenti0_.id as id1_1_0_,
waiterenti0_.name as name2_1_0_,
orders1_.waiter_id as waiter_i4_1_1_,
orders1_.orderNumber as orderNum1_0_1_,
orders1_.orderNumber as orderNum1_0_2_,
orders1_.finalized as finalize2_0_2_,
orders1_.sumOfMoney as sumOfMon3_0_2_,
orders1_.waiter_id as waiter_i4_0_2_
from
WAITERENTITY waiterenti0_
left outer join
ORDERENTITY orders1_
on waiterenti0_.id=orders1_.waiter_id
where
waiterenti0_.id=?
@Entity
@XmlRootElement
@Table(name = "WAITERENTITY")
public class WaiterEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "waiter_id")
private List<OrderEntity> orders = new ArrayList<>();
...
}
@donovanmuller
Copy link
Author

See this comparison

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment